-
Notifications
You must be signed in to change notification settings - Fork 0
/
activityObject.c
299 lines (267 loc) · 7.12 KB
/
activityObject.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* Copyright (C) 2010,2011 by Milan Kazarka
*
* You may distribute under the terms of the Artistic License,
* as specified in the README file.
*
*/
#include "activityObject.h"
#include "config.h"
struct activityObjects *actObjects = NULL;
/** please try not to lock/unlock outside of these functions
*/
int activityObjectsLock( struct activityObjects *objects )
{
if (!objects) return 1;
if (!objects->sem) return 1;
#ifdef _DEBUG
printf("activityObjectsLock\n");
#endif
binarySemaphoreWait(objects->sem->id);
#ifdef _DEBUG
printf("activityObjectsLock finish\n");
#endif
return 0;
}
int activityObjectsUnlock( struct activityObjects *objects )
{
if (!objects) return 1;
if (!objects->sem) return 1;
#ifdef _DEBUG
printf("activityObjectsUnlock\n");
#endif
binarySemaphorePost(objects->sem->id);
#ifdef _DEBUG
printf("activityObjectsUnlock finish\n");
#endif
return 0;
}
/** should be called by a controller, not by applications themselves
*/
struct activityObjects *activityObjectsInit( )
{
#ifdef _DEBUG
printf("activityObjectsInit\n");
#endif
if (actObjects) return actObjects;
struct activityObjects *objects =
(struct activityObjects*)malloc(sizeof(struct activityObjects));
actObjects = objects;
objects->shm = (struct rootShm*)malloc(sizeof(struct rootShm));
objects->sem = (struct rootSem*)malloc(sizeof(struct rootSem));
// todo, should create a unified interface for activities & others
os_rshmAlloc(objects->shm,_ACTIVITY_OBJECTS_STORAGE,_ACTIVITY_TYPE_CUSTOM,(unsigned char*)"OBJECTS");
os_rsemInit(objects->sem);
os_rsemLog(objects->sem,"activity/CUSTOM_OBJECTS");
void *shmdata = shmat(objects->shm->id,0,0);
if (shmdata)
{
memset(shmdata,0x00,_ACTIVITY_OBJECTS_STORAGE);
shmdt(shmdata);
}
else
printf("drink red bull and find a bug kid\n");
#ifdef _DEBUG
printf("activityObjectsInit shmid(%d) semid(%d)\n",
objects->shm->id,objects->sem->id);
#endif
return objects;
}
/** get the data to create a clone of the proto activityObjects
*/
struct activityObjects *activityObjectsClone( )
{
if (actObjects) return actObjects;
struct activityObjects *objects =
(struct activityObjects*)malloc(sizeof(struct activityObjects));
objects->shm = (struct rootShm*)malloc(sizeof(struct rootShm));
objects->sem = (struct rootSem*)malloc(sizeof(struct rootSem));
FILE *fd = NULL;
unsigned char line[256];
unsigned char tmp[256];
if ((fd = fopen("activity/CUSTOM_OBJECTS","r"))==NULL)
{
#ifdef _DEBUG
printf("activityInit could not open file(activity/CUSTOM_OBJECTS)\n");
#endif
}
else
{
while(1)
{
fgets((char*)line,256,fd);
if (feof(fd))
break;
if (line[strlen((char*)line)-1])
{
line[strlen((char*)line)-1] = 0x00;
}
if (strlen((char*)line)>6)
{
if (strncmp((char*)line,"SHMID",5)==0)
{
#ifdef _DEBUG
printf("\tSHMID\n");
#endif
strncpy((char*)tmp,(char*)line+6,strlen((char*)line)-6);
tmp[strlen((char*)line)-6] = 0x00;
objects->shm->id = atoi((char*)tmp);
}
if (strncmp((char*)line,"SHMSZ",5)==0)
{
#ifdef _DEBUG
printf("\tSHMSZ\n");
#endif
strncpy((char*)tmp,(char*)line+6,strlen((char*)line)-6);
tmp[strlen((char*)line)-6] = 0x00;
objects->shm->size = atoi((char*)tmp);
}
if (strncmp((char*)line,"SEMID",5)==0)
{
#ifdef _DEBUG
printf("\tSEMID\n");
#endif
strncpy((char*)tmp,(char*)line+6,strlen((char*)line)-6);
tmp[strlen((char*)line)-6] = 0x00;
objects->sem->id = atoi((char*)tmp);
}
}
}
fclose(fd);
}
return objects;
}
int activityObjectInit( struct activityObject *object )
{
if (!object)
return 1;
#ifdef _DEBUG
printf("activityObjectInit\n");
#endif
object->active = 0x02;
object->size = sizeof(struct activityObject);
object->name[0] = 0x00;
object->fastData[0] = 0x00;
return 0;
}
int activityObjectRegister( struct activityObjects *objects, unsigned char *name )
{
#ifdef _DEBUG
printf("activityObjectRegister shmid(%d) semid(%d)\n",
objects->shm->id,objects->sem->id);
#endif
if (!objects || !name)
return 1;
binarySemaphoreWait(objects->sem->id);
void *shmdata = shmat(objects->shm->id,0,0);
int pos = 0;
if (shmdata)
{
unsigned char tmp[1];
tmp[0] = 0x00;
while(1)
{
if (memcmp((void*)shmdata+pos,(void*)tmp,1)==0)
{
#ifdef _DEBUG
printf("activityObjectRegister found free position\n");
#endif
break;
}
else
{
pos += sizeof(struct activityObject);
}
}
#ifdef _DEBUG
printf("activityObjectRegister at(%d)\n",pos);
#endif
struct activityObject newObject;
activityObjectInit(&newObject);
strcpy((char*)newObject.name,(char*)name);
memcpy(shmdata+pos,&newObject,sizeof(struct activityObject));
shmdt(shmdata);
}
binarySemaphorePost(objects->sem->id);
return 0;
}
int activityObjectClone( struct activityObjects *objects, unsigned char *name, struct activityObject *clone )
{
binarySemaphoreWait(objects->sem->id);
void *shmdata = shmat(objects->shm->id,0,0);
int pos = 0;
int ret = 1;
if (shmdata)
{
unsigned char tmp[1];
tmp[0] = 0x00;
struct activityObject *obj = NULL;
while(1)
{
if (pos+sizeof(struct activityObject)>=_ACTIVITY_OBJECTS_STORAGE)
break;
if (memcmp((void*)shmdata+pos,(void*)tmp,1)==0)
break;
else
{
obj = shmdata+pos;
if (obj->name[0]!=0x00)
{
if (strncmp((char*)obj->name,(char*)name,strlen((char*)name))==0)
{
memcpy(clone,obj,sizeof(struct activityObject));
ret = 0;
break;
}
}
pos += sizeof(struct activityObject);
}
}
shmdt(shmdata);
}
binarySemaphorePost(objects->sem->id);
return ret;
}
int activityObjectSet( struct activityObjects *objects, unsigned char *name, struct activityObject *from )
{
binarySemaphoreWait(objects->sem->id);
void *shmdata = shmat(objects->shm->id,0,0);
int pos = 0;
int ret = 1;
if (shmdata)
{
unsigned char tmp[1];
tmp[0] = 0x00;
struct activityObject *obj = NULL;
while(1)
{
if (pos+sizeof(struct activityObject)>=_ACTIVITY_OBJECTS_STORAGE)
break;
if (memcmp((void*)shmdata+pos,(void*)tmp,1)==0)
break;
else
{
obj = shmdata+pos;
if (obj->name[0]!=0x00)
{
if (strncmp((char*)obj->name,(char*)name,strlen((char*)name))==0)
{
memcpy(obj,from,sizeof(struct activityObject));
ret = 0;
break;
}
}
pos += sizeof(struct activityObject);
}
}
shmdt(shmdata);
}
binarySemaphorePost(objects->sem->id);
return ret;
}
int activityObjectGet( struct activityObjects *objects, unsigned char *name )
{
if (!objects || !name)
return 1;
return 0;
}