arexxd.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*
  2. * Linux Interfece for Arexx Data Loggers
  3. *
  4. * (c) 2011-2012 Martin Mares <mj@ucw.cz>
  5. */
  6. #include <stdio.h>
  7. #include <stdarg.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <fcntl.h>
  12. #include <math.h>
  13. #include <time.h>
  14. #include <getopt.h>
  15. #include <syslog.h>
  16. #include <signal.h>
  17. #include <sys/stat.h>
  18. #include <libusb-1.0/libusb.h>
  19. #include <rrd.h>
  20. #define DEFAULT_LOG_DIR "/var/log/arexxd"
  21. typedef unsigned char byte;
  22. static libusb_context *usb_ctxt;
  23. static libusb_device_handle *devh;
  24. static int use_syslog;
  25. static int debug_mode;
  26. static int debug_packets;
  27. static int debug_raw_data;
  28. static int debug_usb;
  29. static char *log_dir = DEFAULT_LOG_DIR;
  30. static void die(char *fmt, ...)
  31. {
  32. va_list args;
  33. va_start(args, fmt);
  34. if (use_syslog)
  35. vsyslog(LOG_CRIT, fmt, args);
  36. else {
  37. vfprintf(stderr, fmt, args);
  38. fprintf(stderr, "\n");
  39. }
  40. va_end(args);
  41. exit(1);
  42. }
  43. static void log_error(char *fmt, ...)
  44. {
  45. va_list args;
  46. va_start(args, fmt);
  47. if (use_syslog)
  48. vsyslog(LOG_ERR, fmt, args);
  49. else {
  50. vfprintf(stderr, fmt, args);
  51. fprintf(stderr, "\n");
  52. }
  53. va_end(args);
  54. }
  55. static void log_info(char *fmt, ...)
  56. {
  57. va_list args;
  58. va_start(args, fmt);
  59. if (use_syslog)
  60. vsyslog(LOG_INFO, fmt, args);
  61. else {
  62. vfprintf(stderr, fmt, args);
  63. fprintf(stderr, "\n");
  64. }
  65. va_end(args);
  66. }
  67. static void log_pkt(char *fmt, ...)
  68. {
  69. if (!debug_packets)
  70. return;
  71. va_list args;
  72. va_start(args, fmt);
  73. vprintf(fmt, args);
  74. va_end(args);
  75. }
  76. /*** RRD interface ***/
  77. #define MAX_ARGS 20
  78. #define MAX_ARG_SIZE 1024
  79. static int arg_cnt;
  80. static char *arg_ptr[MAX_ARGS+1];
  81. static char arg_buf[MAX_ARG_SIZE];
  82. static int arg_pos;
  83. static void arg_new(void)
  84. {
  85. arg_cnt = 1;
  86. arg_pos = 0;
  87. arg_ptr[0] = "rrdtool";
  88. }
  89. static void arg_push(const char *fmt, ...)
  90. {
  91. if (arg_cnt >= MAX_ARGS)
  92. die("MAX_ARGS exceeded");
  93. va_list va;
  94. va_start(va, fmt);
  95. int len = 1 + vsnprintf(arg_buf + arg_pos, MAX_ARG_SIZE - arg_pos, fmt, va);
  96. if (arg_pos + len > MAX_ARG_SIZE)
  97. die("MAX_ARG_SIZE exceeded");
  98. arg_ptr[arg_cnt++] = arg_buf + arg_pos;
  99. arg_ptr[arg_cnt] = NULL;
  100. arg_pos += len;
  101. }
  102. static void rrd_point(time_t t, const char *name, double val, char *unit)
  103. {
  104. char rr_name[256];
  105. snprintf(rr_name, sizeof(rr_name), "sensor-%s.rrd", name);
  106. struct stat st;
  107. if (stat(rr_name, &st) < 0 || !st.st_size) {
  108. // We have to create the RRD
  109. log_info("Creating %s", rr_name);
  110. arg_new();
  111. arg_push(rr_name);
  112. arg_push("--start");
  113. arg_push("%d", (int) time(NULL) - 28*86400);
  114. arg_push("--step");
  115. arg_push("60");
  116. if (!strcmp(unit, "%RH"))
  117. arg_push("DS:rh:GAUGE:300:0:100");
  118. else if (!strcmp(unit, "ppm"))
  119. arg_push("DS:ppm:GAUGE:300:0:1000000");
  120. else
  121. arg_push("DS:temp:GAUGE:300:-200:200");
  122. arg_push("RRA:AVERAGE:0.25:1:20160"); // Last 14 days with full resolution
  123. arg_push("RRA:AVERAGE:0.25:60:88800"); // Last 10 years with 1h resolution
  124. arg_push("RRA:MIN:0.25:60:88800"); // including minima and maxima
  125. arg_push("RRA:MAX:0.25:60:88800");
  126. rrd_create(arg_cnt, arg_ptr);
  127. if (rrd_test_error()) {
  128. log_error("rrd_create on %s failed: %s", rr_name, rrd_get_error());
  129. rrd_clear_error();
  130. return;
  131. }
  132. }
  133. arg_new();
  134. arg_push(rr_name);
  135. arg_push("%d:%f", t, val);
  136. rrd_update(arg_cnt, arg_ptr);
  137. if (rrd_test_error()) {
  138. log_error("rrd_update on %s failed: %s", rr_name, rrd_get_error());
  139. rrd_clear_error();
  140. }
  141. }
  142. /*** Transforms ***/
  143. #define TIME_OFFSET 946681200 // Timestamp of 2000-01-01 00:00:00
  144. static int data_point_counter; // Since last log message
  145. static double correct_point(int id, double val, const char **name)
  146. {
  147. /*
  148. * Manually calculated corrections and renames for my sensors.
  149. * Replace with your formulae.
  150. */
  151. switch (id) {
  152. case 10415:
  153. *name = "ursarium";
  154. return val - 0.93;
  155. case 10707:
  156. *name = "balcony";
  157. return val - 0.71;
  158. case 19246:
  159. *name = "catarium";
  160. return val + 0.49;
  161. case 19247:
  162. *name = "catarium-rh";
  163. return val;
  164. case 12133:
  165. *name = "outside";
  166. return val + 0.44;
  167. default:
  168. return val;
  169. }
  170. }
  171. static void cooked_point(time_t t, int id, double val, char *unit, int q)
  172. {
  173. char namebuf[16];
  174. snprintf(namebuf, sizeof(namebuf), "%d", id);
  175. const char *name = namebuf;
  176. double val2 = correct_point(id, val, &name);
  177. if (debug_raw_data) {
  178. struct tm tm;
  179. localtime_r(&t, &tm);
  180. char tbuf[64];
  181. strftime(tbuf, sizeof(tbuf), "%Y-%m-%d %H:%M:%S", &tm);
  182. printf("== %s id=%d name=%s val=%.3f val2=%.3f unit=%s q=%d\n", tbuf, id, name, val, val2, unit, q);
  183. }
  184. data_point_counter++;
  185. rrd_point(t, name, val2, unit);
  186. }
  187. static void raw_point(int t, int id, int raw, int q)
  188. {
  189. /*
  190. * The binary blob provided by Arexx contains an embedded XML fragment
  191. * with descriptions of all known sensor types. If you want to see it,
  192. * grep the blob for "<deviceinfo>". The meanings of the parameters are
  193. * as follows:
  194. *
  195. * m1, m2 Device type matches if (raw_sensor_id & m1) == m2
  196. * type Unit measured by the sensor (1=Celsius, 2=RH%, 3=CO2 ppm)
  197. * dm User-visible sensor ID = raw_sensor_id & dm
  198. * i 1 if the raw value is signed
  199. * p[] Coefficients of transformation polynomial (x^0 first)
  200. * vLo, vUp Upper and lower bound on the final value
  201. * scale Scaling function:
  202. * 0 = identity (default)
  203. * 1 = 10^x
  204. * 2 = exp(x)
  205. * 3 = (x < 0) ? 0 : log10(x)
  206. * 4 = (x < 0) ? 0 : log(x)
  207. *
  208. * The raw values are transformed this way:
  209. * - sign-extend if signed
  210. * - apply the transformation polynomial
  211. * - apply the scaling function
  212. * - drop if outside the interval [vLo,vUp]
  213. *
  214. * This function applies the necessary transform for sensors we've
  215. * seen in the wild. We deliberately ignore the "dm" parameter as we want
  216. * to report different channels of a single sensor as multiple sensors.
  217. */
  218. double z = raw;
  219. double hi, lo;
  220. char *unit;
  221. int idhi = id & 0xf000;
  222. if (idhi == 0x1000) {
  223. z = 0.02*z - 273.15;
  224. lo = -200;
  225. hi = 600;
  226. unit = "C";
  227. } else if (idhi == 0x2000) {
  228. if (raw >= 0x8000)
  229. z -= 0x10000;
  230. z /= 128;
  231. lo = -60;
  232. hi = 125;
  233. unit = "C";
  234. } else if (idhi == 0x4000) {
  235. if (!(id & 1)) {
  236. z = z/100 - 39.6;
  237. lo = -60;
  238. hi = 125;
  239. unit = "C";
  240. } else {
  241. z = -2.8e-6*z*z + 0.0405*z - 4;
  242. lo = 0;
  243. hi = 100.1;
  244. unit = "%RH";
  245. }
  246. } else if (idhi == 0x6000) {
  247. if (!(id & 1)) {
  248. if (raw >= 0x8000)
  249. z -= 0x10000;
  250. z /= 128;
  251. lo = -60;
  252. hi = 125;
  253. unit = "C";
  254. } else {
  255. z = -3.8123e-11*z;
  256. z = (z + 1.9184e-7) * z;
  257. z = (z - 1.0998e-3) * z;
  258. z += 6.56;
  259. z = pow(10, z);
  260. lo = 0;
  261. hi = 1e6;
  262. unit = "ppm";
  263. }
  264. } else {
  265. log_error("Unknown sensor type 0x%04x", id);
  266. return;
  267. }
  268. if (z < lo || z > hi) {
  269. log_error("Sensor %d: value %f out of range", id, z);
  270. return;
  271. }
  272. cooked_point(t + TIME_OFFSET, id, z, unit, q);
  273. }
  274. /*** USB interface ***/
  275. static int rx_endpoint, tx_endpoint;
  276. static int parse_descriptors(libusb_device *dev)
  277. {
  278. int err;
  279. struct libusb_config_descriptor *desc;
  280. if (err = libusb_get_active_config_descriptor(dev, &desc)) {
  281. log_error("libusb_get_config_descriptor failed: error %d", err);
  282. return 0;
  283. }
  284. if (desc->bNumInterfaces != 1) {
  285. log_error("Unexpected number of interfaces: %d", desc->bNumInterfaces);
  286. goto failed;
  287. }
  288. const struct libusb_interface *iface = &desc->interface[0];
  289. if (iface->num_altsetting != 1) {
  290. log_error("Unexpected number of alternate interface settings: %d", iface->num_altsetting);
  291. goto failed;
  292. }
  293. const struct libusb_interface_descriptor *ifd = &iface->altsetting[0];
  294. if (ifd->bNumEndpoints != 2) {
  295. log_error("Unexpected number of endpoints: %d", ifd->bNumEndpoints);
  296. goto failed;
  297. }
  298. rx_endpoint = tx_endpoint = -1;
  299. for (int i=0; i<2; i++) {
  300. const struct libusb_endpoint_descriptor *epd = &ifd->endpoint[i];
  301. if (epd->bEndpointAddress & 0x80)
  302. rx_endpoint = epd->bEndpointAddress;
  303. else
  304. tx_endpoint = epd->bEndpointAddress;
  305. }
  306. if (rx_endpoint < 0 || tx_endpoint < 0) {
  307. log_error("Failed to identify endpoints");
  308. goto failed;
  309. }
  310. log_pkt("Found endpoints: rx==%02x tx=%02x\n", rx_endpoint, tx_endpoint);
  311. libusb_free_config_descriptor(desc);
  312. return 1;
  313. failed:
  314. libusb_free_config_descriptor(desc);
  315. return 0;
  316. }
  317. static int find_device(void)
  318. {
  319. libusb_device **devlist;
  320. ssize_t devn = libusb_get_device_list(usb_ctxt, &devlist);
  321. if (devn < 0) {
  322. log_error("Cannot enumerate USB devices: error %d", (int) devn);
  323. return 0;
  324. }
  325. for (ssize_t i=0; i<devn; i++) {
  326. struct libusb_device_descriptor desc;
  327. libusb_device *dev = devlist[i];
  328. if (!libusb_get_device_descriptor(dev, &desc)) {
  329. if (desc.idVendor == 0x0451 && desc.idProduct == 0x3211) {
  330. log_info("Arexx data logger found at usb%d.%d", libusb_get_bus_number(dev), libusb_get_device_address(dev));
  331. if (!parse_descriptors(dev))
  332. continue;
  333. int err;
  334. if (err = libusb_open(dev, &devh)) {
  335. log_error("libusb_open() failed: error %d", err);
  336. goto failed;
  337. }
  338. if (err = libusb_claim_interface(devh, 0)) {
  339. log_error("libusb_claim_interface() failed: error %d", err);
  340. libusb_close(devh);
  341. goto failed;
  342. }
  343. libusb_free_device_list(devlist, 1);
  344. return 1;
  345. }
  346. }
  347. }
  348. failed:
  349. libusb_free_device_list(devlist, 1);
  350. return 0;
  351. }
  352. static void release_device(void)
  353. {
  354. libusb_release_interface(devh, 0);
  355. libusb_reset_device(devh);
  356. libusb_close(devh);
  357. devh = NULL;
  358. }
  359. static void dump_packet(byte *pkt)
  360. {
  361. for (int i=0; i<64; i++) {
  362. if (!(i % 16))
  363. log_pkt("\t%02x:", i);
  364. log_pkt(" %02x", pkt[i]);
  365. if (i % 16 == 15)
  366. log_pkt("\n");
  367. }
  368. }
  369. static void my_msleep(int ms)
  370. {
  371. struct timespec ts = { .tv_sec = ms/1000, .tv_nsec = (ms%1000) * 1000000 };
  372. nanosleep(&ts, NULL);
  373. }
  374. static int send_and_receive(byte *req, byte *reply)
  375. {
  376. if (debug_packets) {
  377. time_t t = time(NULL);
  378. struct tm tm;
  379. localtime_r(&t, &tm);
  380. char tbuf[64];
  381. strftime(tbuf, sizeof(tbuf), "%Y-%m-%d %H:%M:%S", &tm);
  382. log_pkt("## %s\n", tbuf);
  383. }
  384. int err, transferred;
  385. if (err = libusb_bulk_transfer(devh, tx_endpoint, req, 64, &transferred, 200)) {
  386. if (err == LIBUSB_ERROR_TIMEOUT) {
  387. log_pkt(">> xmit timed out\n");
  388. return 0;
  389. }
  390. log_pkt(">> xmit error %d\n", err);
  391. log_error("Transmit error: %d", err);
  392. return err;
  393. }
  394. if (debug_packets) {
  395. log_pkt(">> xmit %d bytes\n", transferred);
  396. dump_packet(req);
  397. }
  398. my_msleep(1);
  399. if (err = libusb_bulk_transfer(devh, rx_endpoint, reply, 64, &transferred, 200)) {
  400. if (err == LIBUSB_ERROR_TIMEOUT) {
  401. log_pkt("<< recv timed out\n");
  402. return 0;
  403. }
  404. log_pkt("<< recv error %d\n", err);
  405. log_error("Receive error: %d", err);
  406. return err;
  407. }
  408. if (debug_packets)
  409. log_pkt("<< recv %d bytes\n", transferred);
  410. while (transferred < 64)
  411. reply[transferred++] = 0xff;
  412. if (debug_packets)
  413. dump_packet(reply);
  414. return 1;
  415. }
  416. static unsigned int get_be16(byte *p)
  417. {
  418. return p[1] | (p[0] << 8);
  419. }
  420. static unsigned int get_le16(byte *p)
  421. {
  422. return p[0] | (p[1] << 8);
  423. }
  424. static unsigned int get_le32(byte *p)
  425. {
  426. return get_le16(p) | (get_le16(p+2) << 16);
  427. }
  428. static void put_le16(byte *p, unsigned int x)
  429. {
  430. p[0] = x;
  431. p[1] = x >> 8;
  432. }
  433. static void put_le32(byte *p, unsigned int x)
  434. {
  435. put_le16(p, x);
  436. put_le16(p+2, x>>16);
  437. }
  438. static int parse_packet(byte *reply)
  439. {
  440. if (reply[0]) {
  441. log_error("Unknown packet type %02x", reply[0]);
  442. return 0;
  443. }
  444. int pos = 1;
  445. int points = 0;
  446. while (pos < 64) {
  447. byte *p = reply + pos;
  448. int len = p[0];
  449. if (!len || len == 0xff)
  450. break;
  451. if (len < 9 || len > 10) {
  452. log_error("Unknown tuple length %02x", len);
  453. break;
  454. }
  455. if (pos + len > 64) {
  456. log_error("Tuple truncated");
  457. break;
  458. }
  459. int id = get_le16(p+1);
  460. int raw = get_be16(p+3);
  461. int t = get_le32(p+5);
  462. int q = (len > 9) ? p[9] : -1;
  463. if (debug_raw_data) {
  464. printf("... %02x: id=%d raw=%d t=%d", len, id, raw, t);
  465. if (len > 9)
  466. printf(" q=%d", q);
  467. printf("\n");
  468. }
  469. raw_point(t, id, raw, q);
  470. pos += len;
  471. points++;
  472. }
  473. return points;
  474. }
  475. static void set_clock(void)
  476. {
  477. byte req[64], reply[64];
  478. memset(req, 0, 64);
  479. req[0] = 4;
  480. time_t t = time(NULL);
  481. put_le32(req+1, t-TIME_OFFSET);
  482. send_and_receive(req, reply);
  483. #if 0
  484. /*
  485. * Original software also sends a packet with type 3 and the timestamp,
  486. * but it does not make any sense, especially as they ignore the sensor
  487. * readings in the answer.
  488. */
  489. req[0] = 3;
  490. send_and_receive(req, reply);
  491. parse_packet(reply);
  492. #endif
  493. }
  494. /*** Main ***/
  495. static sigset_t term_sigs;
  496. static volatile sig_atomic_t want_shutdown;
  497. static void sigterm_handler(int sig __attribute__((unused)))
  498. {
  499. want_shutdown = 1;
  500. }
  501. static void interruptible_msleep(int ms)
  502. {
  503. sigprocmask(SIG_UNBLOCK, &term_sigs, NULL);
  504. my_msleep(ms);
  505. sigprocmask(SIG_BLOCK, &term_sigs, NULL);
  506. }
  507. static const struct option long_options[] = {
  508. { "debug", 0, NULL, 'd' },
  509. { "log-dir", 1, NULL, 'l' },
  510. { "debug-packets", 0, NULL, 'p' },
  511. { "debug-raw", 0, NULL, 'r' },
  512. { "version", 0, NULL, 'V' },
  513. { NULL, 0, NULL, 0 },
  514. };
  515. static void usage(void)
  516. {
  517. fprintf(stderr, "\n\
  518. Usage: arexxd <options>\n\
  519. \n\
  520. Options:\n\
  521. -d, --debug Debug mode (no chdir, no fork, no syslog)\n\
  522. -l, --log-dir=<dir> Directory where all received data should be stored\n\
  523. -p, --debug-packets Log all packets sent and received\n\
  524. -r, --debug-raw Log conversion from raw values\n\
  525. -u, --debug-usb Enable libusb debug messages (to stdout/stderr)\n\
  526. -V, --version Show daemon version\n\
  527. ");
  528. exit(1);
  529. }
  530. int main(int argc, char **argv)
  531. {
  532. int opt;
  533. while ((opt = getopt_long(argc, argv, "dl:pruV", long_options, NULL)) >= 0)
  534. switch (opt) {
  535. case 'd':
  536. debug_mode++;
  537. break;
  538. case 'l':
  539. log_dir = optarg;
  540. break;
  541. case 'p':
  542. debug_packets++;
  543. break;
  544. case 'r':
  545. debug_raw_data++;
  546. break;
  547. case 'u':
  548. debug_usb++;
  549. break;
  550. case 'V':
  551. printf("arexxd " AREXXD_VERSION "\n");
  552. printf("(c) 2011-2012 Martin Mares <mj@ucw.cz>\n");
  553. return 0;
  554. default:
  555. usage();
  556. }
  557. if (optind < argc)
  558. usage();
  559. int err;
  560. if (err = libusb_init(&usb_ctxt))
  561. die("Cannot initialize libusb: error %d", err);
  562. if (debug_usb)
  563. libusb_set_debug(usb_ctxt, 3);
  564. if (!debug_mode) {
  565. if (chdir(log_dir) < 0)
  566. die("Cannot change directory to %s: %m", log_dir);
  567. if (debug_packets || debug_raw_data) {
  568. close(1);
  569. if (open("debug", O_WRONLY | O_CREAT | O_APPEND, 0666) < 0)
  570. die("Cannot open debug log: %m");
  571. setlinebuf(stdout);
  572. }
  573. openlog("arexxd", LOG_NDELAY, LOG_DAEMON);
  574. pid_t pid = fork();
  575. if (pid < 0)
  576. die("fork() failed: %m");
  577. if (pid)
  578. return 0;
  579. setsid();
  580. use_syslog = 1;
  581. }
  582. struct sigaction sa = { .sa_handler = sigterm_handler };
  583. sigaction(SIGTERM, &sa, NULL);
  584. sigaction(SIGINT, &sa, NULL);
  585. sigemptyset(&term_sigs);
  586. sigaddset(&term_sigs, SIGTERM);
  587. sigaddset(&term_sigs, SIGINT);
  588. sigprocmask(SIG_BLOCK, &term_sigs, NULL);
  589. int inited = 0;
  590. while (!want_shutdown) {
  591. if (!find_device()) {
  592. if (!inited) {
  593. inited = 1;
  594. log_error("Data logger not connected, waiting until it appears");
  595. }
  596. interruptible_msleep(30000);
  597. continue;
  598. }
  599. log_info("Listening");
  600. time_t last_sync = 0;
  601. time_t last_show = 0;
  602. int want_stats = 0;
  603. int want_sleep = 0;
  604. data_point_counter = 0;
  605. while (!want_shutdown) {
  606. time_t now = time(NULL);
  607. if (now > last_sync + 900) {
  608. log_info("Synchronizing data logger time");
  609. set_clock();
  610. last_sync = now;
  611. }
  612. if (want_stats && now > last_show + 300) {
  613. log_info("Stats: received %d data points", data_point_counter);
  614. data_point_counter = 0;
  615. last_show = now;
  616. }
  617. byte req[64], reply[64];
  618. memset(req, 0, sizeof(req));
  619. req[0] = 3;
  620. err = send_and_receive(req, reply);
  621. if (err < 0)
  622. break;
  623. want_sleep = 1;
  624. if (err > 0 && parse_packet(reply))
  625. want_sleep = 0;
  626. if (want_sleep) {
  627. interruptible_msleep(4000);
  628. want_stats = 1;
  629. } else
  630. interruptible_msleep(5);
  631. }
  632. log_info("Disconnecting data logger");
  633. release_device();
  634. inited = 0;
  635. interruptible_msleep(10000);
  636. }
  637. log_info("Terminated");
  638. return 0;
  639. }