Holger Hans Peter Freyther | ae73a05 | 2013-05-31 09:51:53 +0200 | [diff] [blame^] | 1 | #include "framing.h" |
| 2 | #include "serial.h" |
| 3 | |
| 4 | #include <stdlib.h> |
| 5 | #include <stdio.h> |
| 6 | #include <string.h> |
| 7 | |
| 8 | #include <sys/stat.h> |
| 9 | #include <fcntl.h> |
| 10 | #include <unistd.h> |
| 11 | #include <errno.h> |
| 12 | |
| 13 | char *DumpBYTEs(unsigned char *p, long n, int nBytesPerRow /* = 16 */, char *szLineSep /* = "\n" */, int bRaw /* = FALSE */, const char *szIndent /* = "" */) |
| 14 | { |
| 15 | long i; |
| 16 | char szBuf[20]; |
| 17 | static char szRes[4 * 1024]; |
| 18 | |
| 19 | szRes[0] = 0; |
| 20 | |
| 21 | if(n == 0) |
| 22 | return szRes; |
| 23 | |
| 24 | memset(szBuf, 0, sizeof(szBuf)); |
| 25 | for(i = 0; i < n; i++) |
| 26 | { |
| 27 | if(i % nBytesPerRow == 0) |
| 28 | { |
| 29 | strcat(szRes, szIndent); |
| 30 | if(!bRaw) |
| 31 | sprintf(szRes + strlen(szRes), "%04d : ", i); |
| 32 | } |
| 33 | |
| 34 | sprintf(szRes + strlen(szRes), "%02X ", p[i]); |
| 35 | szBuf[i % nBytesPerRow] = (char)((p[i] < 128 && p[i] >= ' ') ? p[i] : '.'); |
| 36 | |
| 37 | if((i + 1) % nBytesPerRow == 0) |
| 38 | { |
| 39 | if(bRaw) |
| 40 | sprintf(szRes + strlen(szRes), "%s", szLineSep); |
| 41 | else |
| 42 | sprintf(szRes + strlen(szRes), " %s%s", szBuf, szLineSep); |
| 43 | memset(szBuf, 0, sizeof(szBuf)); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | if(i % nBytesPerRow != 0) |
| 48 | { |
| 49 | if(bRaw) |
| 50 | sprintf(szRes + strlen(szRes), "%s", szLineSep); |
| 51 | else |
| 52 | { |
| 53 | n = nBytesPerRow - i % nBytesPerRow; |
| 54 | for(i = 0; i < n ; i++) |
| 55 | sprintf(szRes + strlen(szRes), " "); |
| 56 | sprintf(szRes + strlen(szRes), " %s%s", szBuf, szLineSep); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return szRes; |
| 61 | } |
| 62 | |
| 63 | static int transmit_packet(int fd, const uint8_t *data, size_t data_len) |
| 64 | { |
| 65 | int out_len, rc; |
| 66 | uint8_t packet[MAX_PACKET * 2]; |
| 67 | |
| 68 | out_len = frame_pack(data, data_len, packet, sizeof(packet)); |
| 69 | if (out_len < 0) { |
| 70 | printf("Failed to pack packet\n"); |
| 71 | return -1; |
| 72 | } |
| 73 | |
| 74 | rc = write(fd, packet, out_len); |
| 75 | if (rc != out_len) { |
| 76 | printf("Short write on packet.\n"); |
| 77 | return -1; |
| 78 | } |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static int do_read(int fd, uint8_t *data) |
| 84 | { |
| 85 | uint8_t buf[MAX_PACKET]; |
| 86 | int rc; |
| 87 | |
| 88 | rc = read(fd, buf, sizeof(buf)); |
| 89 | if (rc <= 0 ) { |
| 90 | printf("Short read!\n"); |
| 91 | exit(EXIT_FAILURE); |
| 92 | } |
| 93 | |
| 94 | rc = frame_unpack(buf, rc, data); |
| 95 | if (rc > 0) { |
| 96 | printf("Got %d data of payload\n", rc); |
| 97 | printf("%s\n", DumpBYTEs(data, rc, 16, "\n", 0, "")); |
| 98 | } |
| 99 | |
| 100 | return rc; |
| 101 | } |
| 102 | |
| 103 | static void do_configure(int fd) |
| 104 | { |
| 105 | static uint8_t timestamp[] = { 0x1D }; |
| 106 | static uint8_t enable_logging[] = { |
| 107 | 0x73, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, |
| 108 | 0x05, 0x00, 0x00, 0x00, 0x28, 0x04, 0x00, 0x00, |
| 109 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 110 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 111 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 112 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 113 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, |
| 114 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 115 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 116 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 117 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x02, 0x00, |
| 118 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 119 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 120 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 121 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 122 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 123 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 124 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 125 | 0x00, 0x00, 0x00, 0x00, 0x00 |
| 126 | }; |
| 127 | static const uint8_t enable_evt_report[] = { |
| 128 | 0x60, 0x01 |
| 129 | }; |
| 130 | static const uint8_t disable_evt_report[] = { |
| 131 | 0x60, 0x00 |
| 132 | }; |
| 133 | static const uint8_t extended_report_cfg[] = { |
| 134 | 0x7D, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 135 | 0x02, 0x00, 0x00, 0x00, |
| 136 | }; |
| 137 | |
| 138 | uint8_t data[MAX_PACKET]; |
| 139 | |
| 140 | /* TODO: introduce a wait for response kind of method */ |
| 141 | transmit_packet(fd, timestamp, sizeof(timestamp)); |
| 142 | do_read(fd, data); |
| 143 | |
| 144 | /* enable the event report */ |
| 145 | // transmit_packet(fd, enable_evt_report, sizeof(enable_evt_report)); |
| 146 | // do_read(fd, data); |
| 147 | transmit_packet(fd, disable_evt_report, sizeof(disable_evt_report)); |
| 148 | do_read(fd, data); |
| 149 | |
| 150 | transmit_packet(fd, extended_report_cfg, sizeof(extended_report_cfg)); |
| 151 | do_read(fd, data); |
| 152 | |
| 153 | /* enable the logging */ |
| 154 | transmit_packet(fd, enable_logging, sizeof(enable_logging)); |
| 155 | do_read(fd, data); |
| 156 | } |
| 157 | |
| 158 | int main(int argc, char **argv) |
| 159 | { |
| 160 | uint8_t data[MAX_PACKET]; |
| 161 | int flags; |
| 162 | |
| 163 | int fd, rc; |
| 164 | if (argc < 2) { |
| 165 | printf("Invoke with %s PATH_TO_SERIAL\n", |
| 166 | argv[0]); |
| 167 | return EXIT_FAILURE; |
| 168 | } |
| 169 | |
| 170 | /* Use nonblock as the device might block otherwise */ |
| 171 | fd = open(argv[1], O_RDWR | O_NOCTTY | O_SYNC | O_NONBLOCK); |
| 172 | if (fd < 0) { |
| 173 | printf("Opening the serial failed: %d/%s\n", |
| 174 | errno, strerror(errno)); |
| 175 | return EXIT_FAILURE; |
| 176 | } |
| 177 | |
| 178 | flags = fcntl(fd, F_GETFL, 0); |
| 179 | if (flags < 0) { |
| 180 | printf("Failed to get the flags.\n"); |
| 181 | return EXIT_FAILURE; |
| 182 | } |
| 183 | |
| 184 | flags &= ~O_NONBLOCK; |
| 185 | rc = fcntl(fd, F_SETFL, flags); |
| 186 | if (rc != 0) { |
| 187 | printf("Failed to set the flags.\n"); |
| 188 | return EXIT_FAILURE; |
| 189 | } |
| 190 | |
| 191 | rc = serial_configure(fd); |
| 192 | if (rc != 0) |
| 193 | return EXIT_FAILURE; |
| 194 | |
| 195 | do_configure(fd); |
| 196 | |
| 197 | while (1) |
| 198 | do_read(fd, data); |
| 199 | } |