meal_pager_retekess_t119.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include "meal_pager_retekess_t119.h"
  2. void customConcat(char* dest, const char* src) {
  3. //FURI_LOG_D(TAG, "adding %s to %s", src, dest);
  4. // Find the end of the destination string
  5. while (*dest != '\0') {
  6. dest++;
  7. }
  8. // Copy characters from src to dest
  9. while (*src != '\0') {
  10. *dest = *src;
  11. dest++;
  12. src++;
  13. }
  14. // Null-terminate the concatenated string
  15. *dest = '\0';
  16. }
  17. char* genRawData(int zero, int one, const char* bits) {
  18. int bitsLen = strlen(bits);
  19. int lineLen = 256; // Adjust the line length as needed
  20. char* line = (char*)malloc(lineLen * sizeof(char));
  21. //FURI_LOG_D(TAG, "bitLen = %u", bitsLen);
  22. // Initialize the line with the first part
  23. //snprintf(line, lineLen, "-6000");
  24. //char* res = (char*)malloc((bitsLen * 4) * sizeof(char));
  25. char* res = (char*)malloc(lineLen * sizeof(char));
  26. res[0] = '\0'; // Null-terminate the result string
  27. //customConcat(res, line);
  28. customConcat(res, "-6000");
  29. // Append bits and create the line
  30. for (int i = 0; i < bitsLen; i++) {
  31. char c = bits[i];
  32. //char p = (i > 0) ? bits[i - 1] : '0';
  33. int t = (c == '0') ? zero : one;
  34. if (i % 2 == 0) {
  35. snprintf(line, lineLen, " %d", t);
  36. } else {
  37. snprintf(line, lineLen, " -%d", t);
  38. }
  39. // Concatenate the line to the result string
  40. //strncat(res, line, bitsLen * 4);
  41. customConcat(res, line);
  42. }
  43. // Append the closing part to the line
  44. //strncat(line, " -6000\n", lineLen);
  45. customConcat(res, " 200 -6000");
  46. //FURI_LOG_D(TAG, "res is: %s", res);
  47. free(line); // Free memory allocated for the line
  48. return res;
  49. }
  50. char* encManchester(const char* bits, int mode) {
  51. // Allocate memory for the result string
  52. char* res = (char*)malloc((strlen(bits) * 2 + 1) * sizeof(char));
  53. int index = 0;
  54. for (int i = 0; bits[i] != '\0'; i++) {
  55. char c = bits[i];
  56. if (c == '0') {
  57. if (mode) {
  58. res[index++] = '1';
  59. res[index++] = '0';
  60. } else {
  61. res[index++] = '0';
  62. res[index++] = '1';
  63. }
  64. } else if (c == '1') {
  65. if (mode) {
  66. res[index++] = '0';
  67. res[index++] = '1';
  68. } else {
  69. res[index++] = '1';
  70. res[index++] = '0';
  71. }
  72. } else {
  73. // Handle 'EE' case (error)
  74. res[index++] = 'E';
  75. res[index++] = 'E';
  76. }
  77. }
  78. // Null-terminate the result string
  79. res[index] = '\0';
  80. return res;
  81. }
  82. void uint32ToBinaray(uint32_t number, char* str, int8_t length) {
  83. int i = 0;
  84. length--; // count length without 0
  85. for (i = length; i >= 0; i--) {
  86. // Bitwise AND extration of the i-th bit
  87. int bit = (number >> i) & 1;
  88. // convert the bit to a character of 1 or 0
  89. str[length - i] = bit + '0';
  90. }
  91. // Terminate the string
  92. str[length+1] = '\0';
  93. }
  94. void reverse(char* str) {
  95. int length = strlen(str);
  96. int start = 0;
  97. int end = length - 1;
  98. while (start < end) {
  99. char temp = str[start];
  100. str[start] = str[end];
  101. str[end] = temp;
  102. start++;
  103. end--;
  104. }
  105. }
  106. static void meal_pager_retekess_t119_generate_pager(void* context, char* stationId, uint32_t pager, FlipperFormat* ff) {
  107. Meal_Pager* app = context;
  108. char pagerId[11];
  109. //char stationPagerId[25];
  110. //char fullId[25];
  111. char* fullId = (char*)malloc(25 * sizeof(char));
  112. uint32_t action = 0; // 0 = ring, 1 = mute
  113. char actionId[2];
  114. //char action[2];
  115. //action[0] = '0'; // 0 = ring, 1 = mute
  116. //action[1] = '\0';
  117. //FURI_LOG_D(TAG, "Generating T119 Data for Pager %lu", pager);
  118. app->current_pager = pager;
  119. meal_pager_transmit_model_set_pager(app->meal_pager_transmit, app->current_pager);
  120. uint32ToBinaray(pager, pagerId, 10);
  121. uint32ToBinaray(action, actionId, 1);
  122. reverse(pagerId);
  123. reverse(actionId);
  124. //FURI_LOG_D(TAG, "Station Bin: %s", stationId);
  125. //FURI_LOG_D(TAG, "Pager Bin: %s", pagerId);
  126. //FURI_LOG_D(TAG, "Action Bin: %s", actionId);
  127. customConcat(fullId, stationId);
  128. customConcat(fullId, pagerId);
  129. //FURI_LOG_D(TAG, "Result %s", fullId);
  130. //FURI_LOG_D(TAG, "Station & Pager: %s", stationPagerId);
  131. //FURI_LOG_D(TAG, "Station & Pager: %s", stationPagerId);
  132. customConcat(fullId, actionId);
  133. //FURI_LOG_D(TAG, "CustomConcat: %s", fullId);
  134. //FURI_LOG_D(TAG, "Station & Pager & Action: %s", fullId);
  135. char* manchester = encManchester(fullId, 0);
  136. //FURI_LOG_D(TAG, "Manchester: %s", manchester);
  137. char* rawSignal = genRawData(200, 600, manchester);
  138. //FURI_LOG_D(TAG, "RAW_Data: %s", rawSignal);
  139. flipper_format_write_string_cstr(ff, "RAW_Data", rawSignal);
  140. free(manchester);
  141. free(rawSignal);
  142. }
  143. static void meal_pager_retekess_t119_generate_station(void* context, uint32_t station, FlipperFormat* ff) {
  144. Meal_Pager* app = context;
  145. FURI_LOG_D(TAG, "Generating T119 Data for Station %lu. Pagers From %lu to %lu", station, app->first_pager, app->last_pager);
  146. app->current_station = station;
  147. app->current_pager = app->first_pager;
  148. char stationId[14];
  149. uint32ToBinaray(station, stationId, 13);
  150. reverse(stationId);
  151. meal_pager_transmit_model_set_station(app->meal_pager_transmit, app->current_station);
  152. for (u_int32_t i = app->current_pager;i <= app->last_pager; i++) {
  153. meal_pager_retekess_t119_generate_pager(app, stationId, i, ff);
  154. //furi_thread_flags_wait(0, FuriFlagWaitAny, 1);
  155. if (app->stop_transmit) {
  156. break;
  157. }
  158. }
  159. }
  160. void meal_pager_retekess_t119_generate_all(void* context) {
  161. Meal_Pager* app = context;
  162. app->current_pager = 1;
  163. app->current_station = app->first_station;
  164. FlipperFormat* ff = meal_pager_save_subghz_buffer_file_start(app);
  165. for (u_int32_t i = app->current_station;i <= app->last_station; i++) {
  166. meal_pager_retekess_t119_generate_station(app, i, ff);
  167. //furi_thread_flags_wait(0, FuriFlagWaitAny, 100);
  168. if (app->stop_transmit) {
  169. break;
  170. }
  171. }
  172. meal_pager_save_subghz_buffer_stop(app, ff);
  173. }