00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <opensdkAPI.h>
00023 #include <OVirtualRobotComm.h>
00024 #include <main.h>
00025 #include <Platform.h>
00026 #include <pthread.h>
00027 #include <ctype.h>
00028 #include <glob.h>
00029 #include <sys/time.h>
00030
00031 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
00032
00034 OStatus RegisterServiceEntry(const OServiceEntry& entry, const char *name)
00035 {
00036 OStatus ret = oALREADY_EXIST;
00037
00038 pthread_mutex_lock(&mutex);
00039 if (_objectsEntryList.find(name) == _objectsEntryList.end()) {
00040 _objectsEntryList[name] = entry;
00041 MOD_DATA(ConnectHandlers).push_back(std::pair<int,int>(entry.GetSelector(), 0));
00042 ret = oSUCCESS;
00043 }
00044 pthread_mutex_unlock(&mutex);
00045
00046 return ret;
00047 }
00048
00049
00051 OStatus _sendMessage(OServiceEntry entry, void *msg) {
00052 if ((size_t)entry.GetOID().GetAddress() >= ThreadsList.size()) {
00053 return oFAIL;
00054 }
00055
00056 perThreadStruct* data = ThreadsList[(size_t)entry.GetOID().GetAddress()];
00057
00058 pthread_mutex_lock(&(data->queue_mutex));
00059 data->queue.push(message(entry, msg));
00060 pthread_mutex_unlock(&(data->queue_mutex));
00061
00062 sem_post(&(data->sem));
00063 return oSUCCESS;
00064 }
00065
00066
00067 OStatus GetJointValue(OPrimitiveID id, OJointValue* value)
00068 {
00069 memset(value, 0, sizeof(OJointValue));
00070 value->value = VirtualRobotComm_get_sensor_reading(id);
00071 return oSUCCESS;
00072 }
00073
00074
00075 OStatus GetSensorValue(OPrimitiveID id, OSensorValue* value)
00076 {
00077 memset(value, 0, sizeof(OSensorValue));
00078 value->value = VirtualRobotComm_get_sensor_reading(id);
00079 return oSUCCESS;
00080 }
00081
00082
00084 char* resolve_case_insensitive_path(const char *path)
00085 {
00086 char *pattern = (char*)malloc(strlen(path) * 4 + 1);
00087 char *tmp = pattern;
00088
00089 while (*path) {
00090 if (!isalpha(*path)) {
00091 *tmp++ = *path++;
00092 continue;
00093 }
00094
00095 *tmp++ = '[';
00096 *tmp++ = tolower(*path);
00097 *tmp++ = toupper(*path);
00098 *tmp++ = ']';
00099
00100 ++path;
00101 }
00102
00103 *tmp = '\0';
00104
00105 glob_t globbuf;
00106 if (glob(pattern, GLOB_NOSORT, NULL, &globbuf) == 0) {
00107 tmp = strdup(globbuf.gl_pathv[0]);
00108 } else {
00109 tmp = strdup(path);
00110 }
00111
00112 globfree(&globbuf);
00113 return tmp;
00114 }
00115
00116
00117
00118
00119
00120
00121 static time_t diffToSystem = 0;
00122 static sbyte myTimeDif = 0;
00123
00124
00125 sbyte GetTimeDifference(void)
00126 {
00127 return myTimeDif;
00128 }
00129
00130
00131 OStatus SetTimeDifference(sbyte timeDifference)
00132 {
00133 if (timeDifference >= -12 && timeDifference <= 12) {
00134 myTimeDif = timeDifference;
00135 return oSUCCESS;
00136 }
00137
00138 return oFAIL;
00139 }
00140
00141
00142 OStatus SetTime(const OTime& time)
00143 {
00144 if (SetTimeDifference(time.GetTimeDif()) != oSUCCESS) {
00145 return oFAIL;
00146 }
00147
00148 diffToSystem = time.GetClock() - (getSystemTime().seconds - diffToSystem);
00149
00150 return oSUCCESS;
00151 }
00152
00153
00154 SystemTime getSystemTime(void)
00155 {
00156 struct timeval tv;
00157 gettimeofday(&tv, NULL);
00158
00159 return SystemTime(tv.tv_sec - TIME_DIFF_SYSTEM + diffToSystem, tv.tv_usec);
00160 }