emit.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include <stdio.h>
  2. #ifdef __arm__
  3. #include <wiringPiSPI.h>
  4. #endif
  5. #include "home_easy.h"
  6. #include "buffer.h"
  7. #include <malloc.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <errno.h>
  11. /**
  12. * Usage of this program
  13. */
  14. void usage(char** argv)
  15. {
  16. fprintf(stderr, "Chacon command V1.0\nC. Meichel\n2013, October\n");
  17. fprintf(stderr, "Syntaxe : %s [options]\n", argv[0]);
  18. fprintf(stderr, "\t-b button\n\t\tbutton (A-D1-4|G). For instance A1, G, D3\n");
  19. fprintf(stderr, "\t-x:\n\t\tOff (default On)\n");
  20. fprintf(stderr, "\t-r number:\n\t\tnumber of repeatitions\n");
  21. fprintf(stderr, "\t-d id:\n\t\thexadecimal id from your command (13 letters <=> 52 bits)\n");
  22. fprintf(stderr, "\t-v: verbose bits\n");
  23. }
  24. /**
  25. * Main program
  26. *
  27. * @param argc number of arguments passed to the program
  28. * @param argv array of arguments passed to the program
  29. *
  30. * @return status
  31. */
  32. int main(int argc, char** argv)
  33. {
  34. //BIT_BUFFER buffer;
  35. BYTE_BUFFER command;
  36. BYTE_BUFFER encoded;
  37. int i;
  38. char optstring[] = "xvb:d:r:";
  39. int option;
  40. unsigned char number = 1;
  41. unsigned char section = 'A';
  42. char *idString=0;
  43. unsigned char onOff = ON;
  44. unsigned int verbose = 0;
  45. unsigned repeat = 5;
  46. /* reading options */
  47. //opterr=0; /* Pas de message d'erreur automatique */
  48. while ((option = getopt(argc, argv, optstring)) != -1) {
  49. switch (option) {
  50. case 'b':
  51. // Decode the button ie : A3
  52. section = optarg[0] - (optarg[0]<'Z' ? 0 : 'a' - 'A');
  53. if (strlen(optarg)>1) {
  54. number = optarg[1] - '0';
  55. }
  56. break;
  57. case 'r':
  58. // repeatition code
  59. sscanf(optarg, "%d", &repeat);
  60. break;
  61. case 'd':
  62. // command id
  63. idString = (char*)malloc(1+strlen(optarg)/2);
  64. for(i=0; i<strlen(optarg)/2 + strlen(optarg) % 2; i++) {
  65. idString[i] = (optarg[i*2] - (optarg[i*2]<='9' ? '0' : (optarg[i*2]<='Z' ? 'A' : 'a')-10)) << 4;
  66. if (optarg[1+i*2]) {
  67. idString[i] += optarg[1+i*2] - (optarg[1+i*2]<='9' ? '0' : (optarg[1+i*2]<='Z' ? 'A' : 'a')-10);
  68. }
  69. }
  70. break;
  71. case 'x':
  72. // OFF ?
  73. onOff = OFF;
  74. break;
  75. case 'v':
  76. // Show the bits to send ?
  77. verbose = 1;
  78. break;
  79. default:
  80. usage(argv);
  81. return 0;
  82. break;
  83. }
  84. }
  85. // No ID specified, it will be "00 00 00 00 00 00 0"
  86. if (!idString) {
  87. idString = (char*)malloc(7);
  88. memset(idString, 0, 7);
  89. }
  90. // Show informations
  91. if (section == 'G') {
  92. printf("Sending command G: %s\n", (onOff == OFF) ? "OFF" : "ON");
  93. } else {
  94. printf("Sending command %c%d: %s\n", section, number, (onOff == OFF) ? "OFF" : "ON");
  95. }
  96. // Display mor information
  97. if (verbose) {
  98. command = createHomeEasyCommand(idString, section, number, onOff);
  99. printfByteBuffer(command);
  100. printf("Code to emit:\n");
  101. encoded = homeEasyEncode(&command);
  102. printfByteBuffer(encoded);
  103. }
  104. // Send the data
  105. initIO();
  106. sendHomeEasyCommand(idString, section, number, onOff, repeat);
  107. // release the memory
  108. destroyByteBuffer(command);
  109. destroyByteBuffer(encoded);
  110. return 0;
  111. }