cphidgetsbc.c
Go to the documentation of this file.
00001 #include "stdafx.h"
00002 #include "cphidgetsbc.h"
00003 #include "cphidget.h"
00004 #include "cthread.h"
00005 #include "cphidgetlist.h"
00006 #include "csocket.h"
00007 #include "zeroconf.h"
00008 
00009 //PhidgetSBCManager
00010 
00011 //Private
00012 void CPhidgetSBCManager_free(void *arg)
00013 {
00014         CPhidgetSBCManagerHandle sbcm = (CPhidgetSBCManagerHandle)arg;
00015         
00016         if(!sbcm) return;
00017         free(sbcm); sbcm = NULL;
00018         return;
00019 }
00020 
00021 //Public
00022 int CCONV CPhidgetSBCManager_create(CPhidgetSBCManagerHandle *sbcm)
00023 {
00024         CPhidgetSBCManagerHandle sbcmtemp = 0;
00025         
00026         TESTPTR(sbcm)
00027         if(!(sbcmtemp = (CPhidgetSBCManagerHandle)malloc(sizeof(CPhidgetSBCManager))))
00028                 return EPHIDGET_NOMEMORY;
00029         ZEROMEM(sbcmtemp, sizeof(CPhidgetSBCManager));
00030 
00031         *sbcm = sbcmtemp;
00032         return EPHIDGET_OK;
00033 }
00034 
00035 int CCONV CPhidgetSBCManager_stop(CPhidgetSBCManagerHandle sbcm)
00036 {
00037         TESTPTR(sbcm)
00038 
00039         if(sbcm->state == PHIDGETMANAGER_ACTIVE)
00040         {
00041                 sbcm->state = PHIDGETMANAGER_INACTIVE;
00042                 unregisterSBCManager(sbcm);
00043         }
00044 
00045         return EPHIDGET_OK;
00046 }
00047 
00048 int CCONV CPhidgetSBCManager_delete(CPhidgetSBCManagerHandle sbcm)
00049 {
00050         CPhidgetSBCManager_free(sbcm);
00051         return EPHIDGET_OK;
00052 }
00053 
00054 int CCONV CPhidgetSBCManager_set_OnAttach_Handler(CPhidgetSBCManagerHandle sbcm, int (CCONV *fptr)(CPhidgetSBCHandle sbc, void *userPtr), void *userPtr)
00055 {
00056         TESTPTR(sbcm)
00057         sbcm->fptrAttachChange = fptr; 
00058         sbcm->fptrAttachChangeptr = userPtr; 
00059         return EPHIDGET_OK; 
00060 }
00061 
00062 int CCONV CPhidgetSBCManager_set_OnDetach_Handler(CPhidgetSBCManagerHandle sbcm, int (CCONV *fptr)(CPhidgetSBCHandle sbc, void *userPtr), void *userPtr)
00063 {
00064         TESTPTR(sbcm)
00065         sbcm->fptrDetachChange = fptr; 
00066         sbcm->fptrDetachChangeptr = userPtr; 
00067         return EPHIDGET_OK; 
00068 }
00069 
00070 int CCONV CPhidgetSBCManager_set_OnError_Handler(CPhidgetSBCManagerHandle sbcm, 
00071         int(CCONV *fptr)(CPhidgetSBCManagerHandle, void *, int, const char *), void *userPtr)
00072 {
00073         TESTPTR(sbcm)
00074         sbcm->fptrError = fptr;
00075         sbcm->fptrErrorptr = userPtr;
00076         return EPHIDGET_OK;
00077 }
00078 
00079 int CCONV CPhidgetSBCManager_getAttachedSBCs(CPhidgetSBCManagerHandle sbcm, CPhidgetSBCHandle *sbcArray[], int *count)
00080 {
00081         TESTPTRS(sbcArray, count)
00082         TESTPTR(sbcm)
00083 
00084         return EPHIDGET_UNSUPPORTED; 
00085 }
00086 
00087 
00088 //PhidgetSBC
00089 
00090 //Private
00091 int CCONV CPhidgetSBC_areExtraEqual(void *arg1, void *arg2)
00092 {
00093         CPhidgetSBCHandle sbc1 = (CPhidgetSBCHandle)arg1;
00094         CPhidgetSBCHandle sbc2 = (CPhidgetSBCHandle)arg2;
00095         
00096         TESTPTRS(sbc1, sbc2)
00097         
00098         if(!strcmp(sbc1->mac, sbc2->mac) 
00099            && !strcmp(sbc1->fversion, sbc2->fversion) 
00100            && sbc1->hversion == sbc2->hversion
00101            && !strcmp(sbc1->hostname?sbc1->hostname:"", sbc2->hostname?sbc2->hostname:""))
00102                 return PTRUE;
00103         return PFALSE;
00104 }
00105 
00106 int CCONV CPhidgetSBC_areEqual(void *arg1, void *arg2)
00107 {
00108         CPhidgetSBCHandle sbc1 = (CPhidgetSBCHandle)arg1;
00109         CPhidgetSBCHandle sbc2 = (CPhidgetSBCHandle)arg2;
00110         
00111         if(!sbc1 || !sbc2)
00112                 return PFALSE;
00113                 
00114         if(!strcmp(sbc1->mac, sbc2->mac))
00115                 return PTRUE;
00116         return PFALSE;
00117 }
00118 
00119 void CCONV CPhidgetSBC_free(void *arg)
00120 {
00121         CPhidgetSBCHandle sbc = (CPhidgetSBCHandle)arg;
00122         if (!sbc)
00123                 return;
00124 
00125         CPhidgetRemote_free(sbc->networkInfo);
00126 
00127         free(sbc); sbc = NULL;
00128         return;
00129 }
00130 
00131 //Public
00132 int CCONV CPhidgetSBC_create(CPhidgetSBCHandle *sbc)
00133 {
00134         CPhidgetSBCHandle sbctemp = 0;
00135         
00136         TESTPTR(sbc)
00137         if(!(sbctemp = (CPhidgetSBCHandle)malloc(sizeof(CPhidgetSBC))))
00138                 return EPHIDGET_NOMEMORY;
00139         ZEROMEM(sbctemp, sizeof(CPhidgetSBC));
00140         
00141         // Version 1 doesn't support hostname variable
00142         sbctemp->txtver = 1;
00143 
00144         *sbc = sbctemp;
00145         return EPHIDGET_OK;
00146 }
00147 
00148 int CCONV CPhidgetSBC_delete(CPhidgetSBCHandle sbc)
00149 {
00150         CPhidgetSBC_free(sbc); sbc = NULL;
00151         return EPHIDGET_OK;
00152 }
00153 
00154 int CCONV CPhidgetSBC_getFirmwareVersion(CPhidgetSBCHandle sbc, const char **version)
00155 {
00156         TESTPTRS(sbc, version)
00157 
00158         *version = (char *)sbc->fversion;
00159         return EPHIDGET_OK;
00160 }
00161 
00162 int CCONV CPhidgetSBC_getHardwareVersion(CPhidgetSBCHandle sbc, int *version)
00163 {
00164         TESTPTRS(sbc, version)
00165 
00166         *version = sbc->hversion;
00167         return EPHIDGET_OK;
00168 }
00169 
00170 int CCONV CPhidgetSBC_getMacAddress(CPhidgetSBCHandle sbc, const char **mac)
00171 {
00172         TESTPTRS(sbc, mac)
00173 
00174         *mac = (char *)sbc->mac;
00175         return EPHIDGET_OK;
00176 }
00177 
00178 int CCONV CPhidgetSBC_getDeviceName(CPhidgetSBCHandle sbc, const char **name)
00179 {
00180         TESTPTRS(sbc, name)
00181 
00182         *name = (char *)sbc->deviceName;
00183         return EPHIDGET_OK;
00184 }
00185 
00186 int CCONV CPhidgetSBC_getHostname(CPhidgetSBCHandle sbc, const char **hostname)
00187 {
00188         TESTPTRS(sbc, hostname)
00189 
00190 #ifdef USE_ZEROCONF
00191         if(sbc->txtver >= 2)
00192         {
00193                 refreshZeroconfSBC(sbc);
00194                 *hostname = (char *)sbc->hostname;
00195                 return EPHIDGET_OK;
00196         }
00197         else
00198 #endif
00199         {
00200                 *hostname = NULL;
00201                 return EPHIDGET_UNSUPPORTED;
00202         }
00203 }
00204 
00205 int CCONV CPhidgetSBC_getAddress(CPhidgetSBCHandle sbc, const char **ipAddr)
00206 {
00207         TESTPTRS(sbc, ipAddr)
00208         if (!sbc->networkInfo)
00209                 return EPHIDGET_NETWORK_NOTCONNECTED;
00210         if(!sbc->networkInfo->mdns) //not mdns
00211         {
00212                 return EPHIDGET_UNEXPECTED;
00213         }
00214 #ifdef USE_ZEROCONF
00215         if(getZeroconfHostPort(sbc->networkInfo))
00216                 return EPHIDGET_NETWORK;
00217         if(!sbc->networkInfo->zeroconf_host)
00218         {
00219                 return EPHIDGET_NETWORK;
00220         }
00221         *ipAddr = (char *)sbc->networkInfo->zeroconf_host;
00222         return EPHIDGET_OK;
00223 #else
00224         return EPHIDGET_UNEXPECTED;
00225 #endif
00226 }
00227 
00228 int CCONV CPhidgetSBC_getIPAddressList(CPhidgetSBCHandle sbc, long *list, unsigned int *size)
00229 {
00230         const char *addr;
00231         struct hostent *addr_lookup;
00232         CPhidgetSBC_getAddress(sbc, &addr);
00233         /* this will resolve to an IP address, including .local hostnames (for SBC, because it can't resolve .local hostnames on its own) */
00234 #ifdef ZEROCONF_LOOKUP
00235         addr_lookup = mdns_gethostbyname(addr);
00236 #else
00237         addr_lookup = gethostbyname(addr);
00238 #endif
00239         if (addr_lookup == NULL)
00240         {
00241            return EPHIDGET_UNKNOWNVAL;
00242         }
00243         else
00244         {
00245            unsigned int i = 0;
00246            while ( addr_lookup -> h_addr_list[i] != NULL && i<*size) {
00247                    struct in_addr inaddr = *(( struct in_addr*)( addr_lookup -> h_addr_list[i]));
00248                    list[i] = inaddr.s_addr;
00249                    i++;
00250            }
00251            *size = i;
00252         }
00253 
00254         return EPHIDGET_OK;
00255 }


pedal_monitor
Author(s): Pedro Mendes
autogenerated on Fri Jun 6 2014 18:37:20