MFAPI
 All Files Functions Macros Pages
mfapi.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2013, Rutgers, The State University of New Jersey
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the organization(s) stated above nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
37 
39 #ifndef MFAPI_H_
40 #define MFAPI_H_
41 #include <stdio.h>
42 #include <unistd.h>
43 #include <sys/socket.h>
44 #include <stdlib.h>
45 #include <errno.h>
46 #include <string.h>
47 #include <linux/un.h>
48 #include <semaphore.h>
49 #include <pthread.h>
50 #include <sys/syscall.h>
51 #include <stdlib.h>
52 #include <time.h>
53 #include <sys/stat.h>
54 
55 #ifndef __linux
56 #include <sys/endian.h>
57 #include <sys/types.h>
58 #endif
59 
60 #include <include/mfflags.h>
61 
62 #include "log.h"
63 #include "errors.h"
64 
65 
66 #ifdef __ANDROID__
67 #warning "Compiling for android"
68 #ifdef ANDROID2
69 #warning "Compiling for android < 2.3.3"
70 #define SER_PATH "/data/mfdemo/mfstack"
71 #define CLI_PATH "/data/mfdemo/" //+ 5(pid)
72 #define SER_DATA_PATH "/data/mfdemo/data" //+ 5 (pid)
73 #define CLI_DATA_PATH "/data/mfdemo/client" //+ 5(pid)
74 #else
75 #warning "Compiling for android > 2.3.3"
76 #define SER_PATH "/sdcard/mfdemo/mfstack"
77 #define CLI_PATH "/sdcard/mfdemo/"
78 #define SER_DATA_PATH "/sdcard/mfdemo/data"
79 #define CLI_DATA_PATH "/sdcard/mfdemo/client"
80 #endif
81 #else
82 #warning "Compiling for linux"
83 #define SER_PATH "/data/mfdemo/mfstack"
84 #define CLI_PATH "/data/mfdemo/"
85 #define SER_DATA_PATH "/data/mfdemo/data"
86 #define CLI_DATA_PATH "/data/mfdemo/client"
87 #endif
88 
89 #define MAX_PAYLOAD_SIZE 1024
90 //#define MAX_PAYLOAD_SIZE 1340
91 #define MAX_CHUNK_SIZE 1024 //number of packets
92 #define MAX_CHUNK_LEN MAX_PAYLOAD_SIZE*MAX_CHUNK_SIZE-ROUTE_HEADER_LEN-TRANS_HEADER_LEN-NEXT_HEADER_RESERVED_SIZE
93 #define LEN_PROF 12
94 #define LEN_OPTS 12
95 #define MAX_ATTACH 10
96 #define ROUTE_HEADER_LEN 60
97 #define NEXT_HEADER_RESERVED_SIZE 60
98 #define TRANS_HEADER_LEN 28
99 #define FIRST_PAYLOAD_SIZE (MAX_PAYLOAD_SIZE-ROUTE_HEADER_LEN-TRANS_HEADER_LEN - NEXT_HEADER_RESERVED_SIZE)
100 
101 #ifdef __cplusplus
102 extern "C" {
103 #endif
104 
105 typedef enum {
106  MSG_TYPE_OPEN = 1,
107  MSG_TYPE_OPEN_REPLY,
108  MSG_TYPE_SEND,
109  MSG_TYPE_SEND_REPLY,
110  MSG_TYPE_SEND_COMPLETE,
111  MSG_TYPE_RECV,
112  MSG_TYPE_RECV_REPLY,
113  MSG_TYPE_CLOSE,
114  MSG_TYPE_CLOSE_REPLY,
115  MSG_TYPE_ATTACH,
116  MSG_TYPE_ATTACH_REPLY,
117  MSG_TYPE_DETACH,
118  MSG_TYPE_DETACH_REPLY,
119  MSG_TYPE_AFTER_CLOSE,
120 } msgType;
121 
122 struct WaitingTIDs{
123  pid_t tid;
124  sem_t wsem;
125  u_int retValue;
126  u_int pktCount;
127  u_int GUID;
128  struct WaitingTIDs *next;
129 };
130 
131 struct Handle{
132  sem_t hsem;
133  u_int UID;
134  int sock;
135  int rsock;
136  int cont;
137  u_int lastReq;
138  pthread_t rect;
139  struct WaitingTIDs *first;
140  struct WaitingTIDs *garbage;
141 };
142 
143 struct Msg{
144  msgType type;
145 };
146 
147 struct MsgOpen {
148  msgType type;
149  u_int UID;
150  //Fixed size is bad... but it will stay like this for now
151  char profile[LEN_PROF];
152  mfflag_t opts;
153  u_int srcGUID;
154 };
155 
156 struct MsgOpenReply {
157  msgType type;
158  u_int retValue;
159 };
160 
161 struct MsgSend {
162  msgType type;
163  u_int UID;
164  u_int reqID;
165  u_int size;
166  int dst_GUID;
167  mfflag_t opts;
168 };
169 
170 struct MsgSendReply {
171  msgType type;
172  u_int UID;
173  u_int reqID;
174  u_int retValue;
175 };
176 
177 struct MsgSendComplete {
178  msgType type;
179  u_int UID;
180  u_int reqID;
181 };
182 
183 struct MsgRecv {
184  msgType type;
185  u_int UID;
186  u_int reqID;
187  char blk;
188  u_int size;
189 };
190 
191 struct MsgRecvReply {
192  msgType type;
193  u_int UID;
194  u_int reqID;
195  u_int retValue;
196  u_int pktCount;
197  u_int sGUID;
198 };
199 
200 struct MsgAttach {
201  msgType type;
202  u_int UID;
203  u_int reqID;
204  u_int GUIDs[MAX_ATTACH];
205  u_int nGUID;
206 };
207 
208 struct MsgAttachReply {
209  msgType type;
210  u_int UID;
211  u_int reqID;
212  u_int retValue;
213 };
214 
215 struct MsgDetach {
216  msgType type;
217  u_int UID;
218  u_int reqID;
219  u_int GUIDs[MAX_ATTACH];
220  u_int nGUID;
221 };
222 
223 struct MsgDetachReply {
224  msgType type;
225  u_int UID;
226  u_int reqID;
227  u_int retValue;
228 };
229 
230 struct MsgClose {
231  msgType type;
232  u_int UID;
233  u_int reqID;
234 };
235 
236 struct MsgCloseReply {
237  msgType type;
238  u_int UID;
239  u_int reqID;
240  u_int retValue;
241 };
242 
243 struct MsgAfterClose{
244  msgType type;
245 };
246 
248 
249 int mfopen(struct Handle *h, const char *profile, mfflag_t opts, const int src_GUID);
250 int mfsend(struct Handle *h, void *buffer, u_int size, const int dst_GUID, mfflag_t opts);
251 int mfrecv(struct Handle *h, int *sGUID, void *buffer, u_int size, const int *src_GUID, const int n_GUID);
252 int mfrecv_blk(struct Handle *h, int *sGUID, void *buffer, u_int size, const int *src_GUID, const int n_GUID);
253 int mfattach(struct Handle *h, const int *GUIDs, const int nGUID);
254 int mfdetach(struct Handle *h, const int *GUIDs, const int nGUID);
255 //content
256 int mfget(struct Handle *h, void *request, u_int bufSize, void *buffer, u_int size, const char *opts);
257 int mfdo_get(struct Handle *h, const int *dst_GUID, void *request, u_int size);
258 int mfget_response(struct Handle *h, void *buffer, u_int size, const char *opts);
259 int mfclose(struct Handle *h);
260 
261 #ifdef __cplusplus
262 }
263 #endif
264 
265 #endif /* MFAPI_H_*/
Define possible errors occurring in MobilityFirst&#39;s API and stack.
int mfattach(struct Handle *h, const int *GUIDs, const int nGUID)
Definition: mfapi.c:563
int mfrecv_blk(struct Handle *h, int *sGUID, void *buffer, u_int size, const int *src_GUID, const int n_GUID)
Definition: mfapi.c:534
int mfsend(struct Handle *h, void *buffer, u_int size, const int dst_GUID, mfflag_t opts)
Definition: mfapi.c:416
int mfopen(struct Handle *h, const char *profile, mfflag_t opts, const int src_GUID)
Definition: mfapi.c:276
int mfrecv(struct Handle *h, int *sGUID, void *buffer, u_int size, const int *src_GUID, const int n_GUID)
Definition: mfapi.c:549
int mfclose(struct Handle *h)
Definition: mfapi.c:622
int mfdetach(struct Handle *h, const int *GUIDs, const int nGUID)
Definition: mfapi.c:594
API Wiki
MobilityFirst Project