first cut at turnon scripts for EasyTimer v2
[fw/altos] / libaltos / libaltos_windows.c
index 2c3f41e11826cb199a5de41c466976235fab8ed9..f91859ac51e7e3859863965ccaa4b48958f7f5e4 100644 (file)
@@ -3,7 +3,8 @@
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License.
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -58,12 +59,12 @@ log_message(char *fmt, ...)
                char    buffer[4096];
 
                GetLocalTime(&time);
-               sprintf (buffer, "%4d-%02d-%02d %2d:%02d:%02d. ",
+               __ms_sprintf (buffer, "%4d-%02d-%02d %2d:%02d:%02d. ",
                         time.wYear, time.wMonth, time.wDay,
                         time.wHour, time.wMinute, time.wSecond);
                va_start(a, fmt);
 
-               vsprintf(buffer + strlen(buffer), fmt, a);
+               __ms_vsprintf(buffer + strlen(buffer), fmt, a);
                va_end(a);
 
                fputs(buffer, log);
@@ -130,6 +131,16 @@ altos_ftdi_list_start(void)
        return list;
 }
 
+static struct {
+       unsigned int    vid, pid;
+       char    *name;
+} name_map[] = {
+       { .vid = 0xfffe, .pid = 0x000d, .name = "EasyTimer" },
+       { .vid = 0xfffe, .pid = 0x0028, .name = "EasyMega" },
+       { .vid = 0xfffe, .pid = 0x002c, .name = "EasyMotor" },
+       { .name = NULL },
+};
+
 PUBLIC int
 altos_list_next(struct altos_list *list, struct altos_device *device)
 {
@@ -147,6 +158,7 @@ altos_list_next(struct altos_list *list, struct altos_device *device)
        DWORD           friendlyname_len;
        char            instanceid[1024];
        DWORD           instanceid_len;
+       int             i;
 
        dev_info_data.cbSize = sizeof (SP_DEVINFO_DATA);
        while(SetupDiEnumDeviceInfo(list->dev_info, list->index,
@@ -176,11 +188,11 @@ altos_list_next(struct altos_list *list, struct altos_device *device)
                        if (result != 0) {
                                altos_set_last_windows_error();
                        } else {
-                               sscanf((char *) symbolic + sizeof("\\??\\USB#VID_") - 1,
+                               __ms_sscanf((char *) symbolic + sizeof("\\??\\USB#VID_") - 1,
                                       "%04X", &vid);
-                               sscanf((char *) symbolic + sizeof("\\??\\USB#VID_XXXX&PID_") - 1,
+                               __ms_sscanf((char *) symbolic + sizeof("\\??\\USB#VID_XXXX&PID_") - 1,
                                       "%04X", &pid);
-                               sscanf((char *) symbolic + sizeof("\\??\\USB#VID_XXXX&PID_XXXX#") - 1,
+                               __ms_sscanf((char *) symbolic + sizeof("\\??\\USB#VID_XXXX&PID_XXXX#") - 1,
                                       "%d", &serial);
                        }
                        if (vid == 0 || pid == 0 || serial == 0) {
@@ -189,11 +201,11 @@ altos_list_next(struct altos_list *list, struct altos_device *device)
                                                               instanceid,
                                                               sizeof (instanceid),
                                                               &instanceid_len)) {
-                                       sscanf((char *) instanceid + sizeof("USB\\VID_") - 1,
+                                       __ms_sscanf((char *) instanceid + sizeof("USB\\VID_") - 1,
                                               "%04X", &vid);
-                                       sscanf((char *) instanceid + sizeof("USB\\VID_XXXX&PID_") - 1,
+                                       __ms_sscanf((char *) instanceid + sizeof("USB\\VID_XXXX&PID_") - 1,
                                               "%04X", &pid);
-                                       sscanf((char *) instanceid + sizeof("USB\\VID_XXXX&PID_XXXX\\") - 1,
+                                       __ms_sscanf((char *) instanceid + sizeof("USB\\VID_XXXX&PID_XXXX\\") - 1,
                                               "%d", &serial);
                                } else {
                                        altos_set_last_windows_error();
@@ -229,6 +241,23 @@ altos_list_next(struct altos_list *list, struct altos_device *device)
                        altos_set_last_windows_error();
                        continue;
                }
+
+               char *space = friendlyname;
+               while (*space) {
+                       if (*space == ' ') {
+                               *space = '\0';
+                               break;
+                       }
+                       space++;
+               }
+
+               for (i = 0; name_map[i].name; i++) {
+                       if (name_map[i].vid == vid && name_map[i].pid == pid) {
+                               strcpy(friendlyname, name_map[i].name);
+                               break;
+                       }
+               }
+
                device->vendor = vid;
                device->product = pid;
                device->serial = serial;
@@ -638,7 +667,7 @@ static void
 ba2str(BTH_ADDR ba, char *str)
 {
 
-       sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
+       __ms_sprintf(str, "%02X:%02X:%02X:%02X:%02X:%02X",
                get_byte(ba, 0),
                get_byte(ba, 1),
                get_byte(ba, 2),
@@ -652,7 +681,7 @@ str2ba(char *str)
 {
        unsigned int    bytes[6];
 
-       sscanf(str,  "%02x:%02x:%02x:%02x:%02x:%02x",
+       __ms_sscanf(str,  "%02x:%02x:%02x:%02x:%02x:%02x",
               &bytes[0],
               &bytes[1],
               &bytes[2],
@@ -728,6 +757,7 @@ altos_bt_open(struct altos_bt_device *device)
        struct altos_file_windows       *file;
        SOCKADDR_BTH            sockaddr_bth;
        int                     ret;
+       int                     channel = 0;
 
        file = calloc(1, sizeof (struct altos_file_windows));
        if (!file) {
@@ -746,7 +776,12 @@ altos_bt_open(struct altos_bt_device *device)
        memset(&sockaddr_bth, '\0', sizeof (sockaddr_bth));
        sockaddr_bth.addressFamily = AF_BTH;
        sockaddr_bth.btAddr = str2ba(device->addr);
-       sockaddr_bth.port = 1;
+
+       channel = altos_bt_port(device);
+       if (channel == 0)
+               channel = BT_PORT_DEFAULT;
+
+       sockaddr_bth.port = channel;
 
        ret = connect(file->socket, (SOCKADDR *) &sockaddr_bth, sizeof (sockaddr_bth));
 
@@ -754,8 +789,14 @@ altos_bt_open(struct altos_bt_device *device)
                altos_set_last_winsock_error();
                closesocket(file->socket);
                free(file);
+               log_message("Connection attempted to address %s port %d\n", device->addr, sockaddr_bth.port);
                return NULL;
        }
        return &file->file;
 }
 
+void
+altos_pause_one_second(void)
+{
+       Sleep(1000);
+}