emit.c 2.9 KB

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