mock_setup.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include <assert.h> //header file to verify the assumptions made by the program and print a diagnostic message if the assumption is false
  2. #include "src/config.h"
  3. #include "src/controller/controller.h"
  4. #include "src/i2c_comm/i2c_hal.h"
  5. #include <stdio.h>
  6. #include "mock_setup.h"
  7. //Testing:: Declare the extern variables for mock testing, comment it out in production
  8. txPacket tx_packet;
  9. rxPacket rx_packet;
  10. //mock I2C HAL functions which will be used to test the controller functions, since the target board is not available
  11. bool mock_i2c_write(uint8_t const TARGET_ADDRESS)
  12. {
  13. printf("[I2C Mock]Write to target 0x%02X\n", TARGET_ADDRESS);
  14. // Mock implementation of I2C write
  15. return true; // Simulate successful write
  16. }
  17. bool mock_i2c_read(uint8_t const TARGET_ADDRESS)
  18. {
  19. printf("[I2C Mock]Read from target 0x%02X\n", TARGET_ADDRESS);
  20. // Mock implementation of I2C read
  21. return true; // Simulate successful read
  22. }
  23. //Setting Current Mock:
  24. void test_Controller_SetCurrent()
  25. {
  26. // Set the current to a specific value
  27. int current = -1000; // Example value
  28. controller_SetCurrent(0x48, 0, current);
  29. //Debug
  30. printf("Mock command SET: 0x%02X\n", tx_packet.txBuffer[0]);
  31. printf("Mock slot_id: 0x%02X\n", tx_packet.txBuffer[1]);
  32. printf("Mock current: 0x%02X\n", tx_packet.txBuffer[2]);
  33. printf("Mock current: 0x%02X\n", tx_packet.txBuffer[3]);
  34. //assert() checks if the variable is true or false, if False the program gets aborted and a diagnostic message is printed
  35. assert(tx_packet.txBuffer[0] == CMD_SET_CURRENT);
  36. assert(tx_packet.txBuffer[1] == 0);
  37. //Current is sent in two bytes, as Little Endian format
  38. assert(tx_packet.txBuffer[2] == (current & 0xFF)); // LSB
  39. assert(tx_packet.txBuffer[3] == ((current >> 8) & 0xFF)); // MSB
  40. assert(tx_packet.txLen == 4); // 1 byte for command + 1 byte for slot_id + 1 byte for current
  41. printf("[TEST] Set Current: Passed\n");
  42. }
  43. void test_Controller_GetBatteryMeasurement()
  44. {
  45. // Mock data for battery measurement
  46. BatteryMeasurement measurement;
  47. measurement.voltage = 350; // Example voltage
  48. measurement.current = 10; // Example current
  49. measurement.temperature = 25; // Example temperature
  50. measurement.slot_state = SLOT_STATE_OK; // Example slot state
  51. // Check if the function returns success
  52. rx_packet.rxBuffer[0] = measurement.voltage & 0xFF; // Voltage LSB
  53. rx_packet.rxBuffer[1] = (measurement.voltage >> 8) & 0xFF; // Voltage MSB
  54. rx_packet.rxBuffer[2] = measurement.current & 0xFF; // Current LSB
  55. rx_packet.rxBuffer[3] = (measurement.current >> 8) & 0xFF; // Current MSB
  56. rx_packet.rxBuffer[4] = measurement.temperature & 0xFF; // Temperature LSB
  57. rx_packet.rxBuffer[5] = (measurement.temperature >> 8) & 0xFF; // Temperature MSB
  58. rx_packet.rxBuffer[6] = measurement.slot_state & 0xFF; // Slot state LSB
  59. rx_packet.rxBuffer[7] = (measurement.slot_state >> 8) & 0xFF; // Slot state MSB
  60. rx_packet.rxLen = 6;
  61. // Call the function to get battery measurement
  62. bool result= controller_GetBatteryMeasurement(0x48, 0, &measurement);
  63. printf("Mock command GET: 0x%02X\n", tx_packet.txBuffer[0]);
  64. assert(result == true);
  65. assert(measurement.voltage == 350);
  66. assert(measurement.current == 10);
  67. assert(measurement.temperature == 25);
  68. assert(rx_packet.rxLen==6);
  69. printf("[TEST] Get Battery Measurement: Passed\n");
  70. }
  71. I2C_Mock_Interface i2c_mock_interface = {
  72. .write = mock_i2c_write,
  73. .read = mock_i2c_read,
  74. };