TCP Socket Bind API
[platform/upstream/iotivity.git] / resource / csdk / connectivity / api / cautilinterface.h
1 /* ****************************************************************
2  *
3  * Copyright 2016 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 #ifndef CA_UTILS_INTERFACE_H_
22 #define CA_UTILS_INTERFACE_H_
23
24 #include "cacommon.h"
25 #ifdef __ANDROID__
26 #include "jni.h"
27 #endif
28 #ifdef __cplusplus
29 extern "C"
30 {
31 #endif
32
33 /**
34  * this level depends on transmission time.
35  * unicast based UDP will be checked by caretransmission.
36  */
37 typedef enum
38 {
39     HIGH_SPEED = 0,
40     NORMAL_SPEED
41 } CMSpeedLevel_t;
42
43 typedef struct
44 {
45     /** address for all **/
46     char addr[MAX_ADDR_STR_SIZE_CA];
47
48     /** adapter priority of all transmissions. **/
49     CATransportAdapter_t adapter;
50
51     /** level about speed of response. **/
52     CMSpeedLevel_t level;
53 } CMConfigureInfo_t;
54
55 /*
56  * CAUtilConfig_t structure.
57  */
58 typedef struct
59 {
60     CATransportBTFlags_t bleFlags;
61     CMConfigureInfo_t cmInfo;
62 } CAUtilConfig_t;
63
64 #if defined(__TIZEN__)
65
66 #define MAX_IP_LENGTH 46
67
68 typedef struct
69 {
70     /* These 4 values are Kernel dependent and can be extracted only when Kernel is
71     built with enabling to monitor these values*/
72     u_int32_t tcpi_snd_nxt;
73     u_int32_t tcpi_rcv_nxt;
74     u_int32_t tcpi_rcv_wnd;
75     u_int8_t tcpi_rcv_wscale;
76     /*....*/
77     u_int8_t tcpi_src_addr[MAX_IP_LENGTH];
78     u_int32_t tcpi_src_port;
79     u_int8_t tcpi_dst_addr[MAX_IP_LENGTH];
80     u_int32_t tcpi_dst_port;
81     u_int32_t ip_version;
82     void* info;
83 } TCPHeaderInfo;
84
85 typedef struct
86 {
87     u_int8_t tcpi_state;
88     u_int8_t tcpi_ca_state;
89     u_int8_t tcpi_retransmits;
90     u_int8_t tcpi_probes;
91     u_int8_t tcpi_backoff;
92     u_int8_t tcpi_options;
93     u_int8_t tcpi_snd_wscale : 4, tcpi_rcv_wscale : 4;
94
95     /* TCP Keep Alive Offload */
96     u_int32_t tcpi_snd_nxt;
97     u_int32_t tcpi_rcv_nxt;
98     u_int32_t tcpi_rcv_wnd;
99
100     u_int32_t tcpi_rto;
101     u_int32_t tcpi_ato;
102     u_int32_t tcpi_snd_mss;
103     u_int32_t tcpi_rcv_mss;
104
105     u_int32_t tcpi_unacked;
106     u_int32_t tcpi_sacked;
107     u_int32_t tcpi_lost;
108     u_int32_t tcpi_retrans;
109     u_int32_t tcpi_fackets;
110
111     /* Times. */
112     u_int32_t tcpi_last_data_sent;
113     u_int32_t tcpi_last_ack_sent;
114     u_int32_t tcpi_last_data_recv;
115     u_int32_t tcpi_last_ack_recv;
116
117     /* Metrics. */
118     u_int32_t tcpi_pmtu;
119     u_int32_t tcpi_rcv_ssthresh;
120     u_int32_t tcpi_rtt;
121     u_int32_t tcpi_rttvar;
122     u_int32_t tcpi_snd_ssthresh;
123     u_int32_t tcpi_snd_cwnd;
124     u_int32_t tcpi_advmss;
125     u_int32_t tcpi_reordering;
126     u_int32_t tcpi_rcv_rtt;
127     u_int32_t tcpi_rcv_space;
128     u_int32_t tcpi_total_retrans;
129 } TCPInfo;
130
131 #endif
132
133 /**
134  * Callback function type for connection status changes delivery.
135  * @param[out]   info           Remote endpoint information.
136  * @param[out]   isConnected    Current connection status info.
137  */
138 typedef void (*CAConnectionStateChangedCB)(const CAEndpoint_t *info, bool isConnected);
139
140 /**
141  * Callback function type for adapter status changes delivery.
142  * @param[out]   adapter    Transport type information.
143  * @param[out]   enabled    Current adapter status info.
144  */
145 typedef void (*CAAdapterStateChangedCB)(CATransportAdapter_t adapter, bool enabled);
146
147 /**
148  * Register network monitoring callback.
149  * Network status changes are delivered these callback.
150  * @param[in]   adapterStateCB  Adapter state monitoring callback.
151  * @param[in]   connStateCB     Connection state monitoring callback.
152  *
153  * @return  ::CA_STATUS_OK or ::CA_STATUS_FAILED or ::CA_MEMORY_ALLOC_FAILED
154  */
155 CAResult_t CARegisterNetworkMonitorHandler(CAAdapterStateChangedCB adapterStateCB,
156                                            CAConnectionStateChangedCB connStateCB);
157
158 /**
159  * Unregister network monitoring callback.
160  * @param[in]   adapterStateCB  Adapter state monitoring callback.
161  * @param[in]   connStateCB     Connection state monitoring callback.
162  *
163  * @return  ::CA_STATUS_OK or ::CA_STATUS_FAILED or ::CA_MEMORY_ALLOC_FAILED
164  */
165 CAResult_t CAUnregisterNetworkMonitorHandler(CAAdapterStateChangedCB adapterStateCB,
166                                              CAConnectionStateChangedCB connStateCB);
167
168 /**
169  * Set device to handle for auto connection.
170  * @param[in]   address         LE address to set.
171  *
172  * @return  ::CA_STATUS_OK or ::CA_STATUS_FAILED.
173  */
174 CAResult_t CASetAutoConnectionDeviceInfo(const char* address);
175
176 /**
177  * Unset device to handle for auto connection.
178  * @param[in]   address         LE address to unset.
179  *
180  * @return  ::CA_STATUS_OK or ::CA_STATUS_FAILED.
181  */
182 CAResult_t CAUnsetAutoConnectionDeviceInfo(const char* address);
183
184 /**
185  * Set the port number to assign .
186  * @param[in]   adapter     Transport adapter information.
187  * @param[in]   flag        Transport flag information.
188  * @param[in]   port        The port number to use.
189  *
190  * @return  ::CA_STATUS_OK or ::CA_STATUS_FAILED.
191  */
192 CAResult_t CASetPortNumberToAssign(CATransportAdapter_t adapter,
193                                    CATransportFlags_t flag, uint16_t port);
194
195 /**
196  * Get the assigned port number currently.
197  * @param[in]   adapter     Transport adapter information.
198  * @param[in]   flag        Transport flag information.
199  *
200  * @return  assigned port number information.
201  */
202 uint16_t CAGetAssignedPortNumber(CATransportAdapter_t adapter, CATransportFlags_t flag);
203
204 //custom advertisement data setting
205 #if defined(__TIZEN__) && defined(LE_ADAPTER) && defined(BLE_CUSTOM_ADVERTISE)
206
207 /**
208  * Sets Custom Advertisement Data.
209  * @param[in]   data    Advertisment Data buffer.
210  * @param[in]   length  Length of buffer.
211  *
212  * @return  ::CA_STATUS_OK or ::CA_STATUS_FAILED or ::CA_STATUS_INVALID_PARAM
213  */
214 CAResult_t CASetAdvertisementData(const char* data, int length);
215
216 /**
217  * Set function that will be called before starting advertising.
218  * Provided data and length will be used to set advertisement data.
219  * @param[in]  getter     Custom advertisement data getter callback.
220  *
221  */
222 void CASetAdvertisementDataGetter(CAAdvertisementDataGetterCB getter);
223
224 /**
225  * set if advertising should be started automatically.
226  * By default advertising is automatically started.
227  * @param[in]  autoAdvertisement         true for automatic start
228  */
229 void CASetAutoAdvertisement(bool autoAdvertisement);
230
231 /**
232  * Scan and set data for custom bluetooth advertisement.
233  * @param[in]   data                   Array with custom bluetooth advertisement data.
234  * @param[in]   length                 Length of data array.
235  * @return  ::CA_STATUS_OK or ::CA_STATUS_FAILED or ::CA_STATUS_INVALID_PARAM
236  */
237 CAResult_t CASetScanResponseData(const char* data, int length);
238
239 #endif
240
241 #if defined(__TIZEN__)
242 /**
243  * This function sets uri being used for proxy.
244  *
245  * @param uri            NULL terminated resource uri for SAP Proxy.
246  *
247  *
248  * @return  ::CA_STATUS_OK or ::CA_STATUS_INVALID_PARAM
249  */
250 CAResult_t CASetCloudAddressForProxy(const char *uri);
251 /**
252  * This function gets proxy uri.
253  *
254  * @return  resource uri or NULL
255  */
256 const char *CAGetCloudAddressForProxy();
257 #endif
258
259 #ifdef __APPLE__
260 /**
261  * initialize client connection manager
262  */
263 CAResult_t CAUtilClientInitialize();
264
265 /**
266  * terminate client connection manager
267  */
268 CAResult_t CAUtilClientTerminate();
269
270 /**
271  * stop LE scan.
272  * @return  ::CA_STATUS_OK or ::CA_NOT_SUPPORTED
273  */
274 CAResult_t CAUtilStopLEScan();
275
276 /**
277  * start LE scan.
278  * @return  ::CA_STATUS_OK or ::CA_NOT_SUPPORTED
279  */
280 CAResult_t CAUtilStartLEScan();
281
282 /**
283  * Client disconnect.
284  * @return  ::CA_STATUS_OK or ::CA_NOT_SUPPORTED
285  */
286 CAResult_t CAUtilClientDisconnect();
287 #endif
288
289 #if defined(__ANDROID__)
290 /**
291  *  set UUID for setting scanfilter, which will add another scanfilter for BLE scanning in android client based on the UUID or manufacture data provided
292  *  @param[in]   uuid                 service UUID string or manufacture Data
293  *  @param[in]   uuid_type            Type of scanfilter to be added i.e. service UUID based or manufacture data based
294  */
295 void CASetLEScanUuid(char* uuid, int uuid_type);
296 #endif
297
298 #ifdef __ANDROID__
299 /**
300  * initialize util client for android
301  * @param[in]   env                   JNI interface pointer.
302  * @param[in]   jvm                   invocation inferface for JAVA virtual machine.
303  * @param[in]   context               application context.
304  *
305  * @return  ::CA_STATUS_OK or ::CA_STATUS_FAILED or ::CA_MEMORY_ALLOC_FAILED
306  */
307 CAResult_t CAUtilClientInitialize(JNIEnv *env, JavaVM *jvm, jobject context);
308
309 /**
310  * terminate util client for android.
311  * @param[in]   env                   JNI interface pointer.
312  *
313  * @return  ::CA_STATUS_OK or ::CA_STATUS_FAILED or ::CA_MEMORY_ALLOC_FAILED
314  */
315 CAResult_t CAUtilClientTerminate(JNIEnv *env);
316
317 // BT pairing
318 /**
319  * start discovery for BT device which has iotivity UUID.
320  * @param[in]  env              JNI interface pointer.
321  */
322 CAResult_t CAUtilStartScan(JNIEnv *env);
323
324 /**
325  * stop discovery
326  * @param[in]  env              JNI interface pointer.
327  */
328 CAResult_t CAUtilStopScan(JNIEnv *env);
329
330 /**
331  * bonding between devices.
332  * @param[in]  env              JNI interface pointer.
333  * @param[in]  device           bluetooth device object.
334  */
335 CAResult_t CAUtilCreateBond(JNIEnv *env, jobject device);
336
337
338 /**
339  * set callback listener of found device.
340  * @param[in]  listener         callback listener
341  */
342 void CAUtilSetFoundDeviceListener(jobject listener);
343
344 /**
345  * set interval time and working count for LE scan.
346  * @param[in]  intervalTime         interval time(Seconds).
347  * @param[in]  workingCount         working cycle for selected interval time.
348  *
349  * @return  ::CA_STATUS_OK or ::CA_NOT_SUPPORTED
350  */
351 CAResult_t CAUtilSetLEScanInterval(jint intervalTime, jint workingCount);
352
353 /**
354  * stop LE scan.
355  *
356  * @return  ::CA_STATUS_OK or ::CA_NOT_SUPPORTED
357  */
358 CAResult_t CAUtilStopLEScan();
359 #endif
360
361 // BLE util
362 /**
363  * start BLE advertising.
364  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
365  */
366 CAResult_t CAUtilStartLEAdvertising();
367
368 /**
369  * stop BLE advertising.
370  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
371  */
372 CAResult_t CAUtilStopLEAdvertising();
373
374 /**
375  * set CAUtil BT configure.
376  * @param[in]  config       ::CAUtilConfig_t value
377  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
378  */
379 CAResult_t CAUtilSetBTConfigure(CAUtilConfig_t config);
380
381 /**
382  * set CAUtil log preference.
383  * @param[in]  level                     ::CAUtilLogLevel_t value
384  * @param[in]  hidePrivateLogEntries     Private Log Entries.
385  *                                       Example:
386  *                                       true : hide private log.
387  *                                       false : show private log.
388  *                                       (privacy : uid, did, access token, etc)
389  */
390 void CAUtilSetLogLevel(CAUtilLogLevel_t level, bool hidePrivateLogEntries);
391
392 /**
393  * Set multicast time to live value to control the scope of the multicasts.
394  * @param[in]  ttl         To be set to any value from 0 to 255.
395  *                         Example:
396  *                         0: Are restricted to the same host.
397  *                         1: Are restricted to the same subnet.
398  *                         32: Are restricted to the same site.
399  *                         64: Are restricted to the same region.
400  *                         128: Are restricted to the same continent.
401  *                         255: Are unrestricted in scope.
402  *                         We cannot support region, continent and unrestricted in scope.
403  *
404  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
405  */
406 CAResult_t CAUtilSetMulticastTTL(size_t ttl);
407
408 /**
409  * Get multicast time to live value.
410  * @param[out]  ttl        TTL pointer to get the stored multicast time to live.
411  *
412  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
413  */
414 CAResult_t CAUtilGetMulticastTTL(size_t *ttl);
415
416 /**
417  * Disconnect TCP session.
418  * When there is no transmission for a long time.
419  * Some carrier Vendor is blocking data.
420  * Thur, TCP Session is cleaned through this function.
421  * @param[in]   address        Address.
422  * @param[in]   port           Port.
423  * @param[in]   flags          Transport flag.
424  */
425 CAResult_t CAUtilTCPDisconnectSession(const char *address,
426                                       uint16_t port,
427                                       CATransportFlags_t flags);
428
429 /**
430  * Enable or disable skip closing TCP servers
431  * when network interface is down.
432  *
433  * @param[in]   state    true to skip closing TCP servers,
434  *                       otherwise false.
435  */
436 void CAUtilSkipTCPCloseOnInterfaceDown(bool state);
437
438
439 /**
440  * Set a secure endpoint identity with uuid.
441  *
442  * @param[in]   peer     peer information includs IP address and port.
443  * @param[in]   uuid     UUID of target device.
444  *
445  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
446  */
447 CAResult_t CASetSecureEndpointUuid(const CAEndpoint_t *peer, const char *uuid);
448
449 CAResult_t CAUtilStartGattServer();
450 CAResult_t CAUtilStopGattServer();
451
452 #if defined(__TIZEN__)
453 CAResult_t CAGetTCPIPHeader(CATransportAdapter_t adapter, int flag, TCPHeaderInfo* info);
454
455 /**
456  * Enables keep-alive on the given endpoint & configures it with user defined values.
457  *
458  * @param[in]  endpoint    Endpoint on keep-alive needs to be enabled.
459  * @param[in]  time    The number of seconds a connection needs to be idle
460  *                                  before TCP begins sending out keep-alive probes.
461  * @param[in]  cnt    The maximum number of TCP keep-alive probes to send before
462  *                                giving up and killing the connection if no response is obtained from the other end
463  * @param[in]  intvl    The number of seconds between TCP keep-alive probes.
464  *
465  * @return  ::CA_STATUS_OK or Appropriate error code.
466  */
467 CAResult_t CASetTCPKeepAlive(const CAEndpoint_t *endpoint, int time, int cnt, int intvl);
468
469 /**
470  * Disables keep-alive on the given endpoint.
471  *
472  * @param[in]  endpoint    Endpoint on which keep-alive needs to be disabled.
473  *
474  * @return  ::CA_STATUS_OK or Appropriate error code.
475  */
476 CAResult_t CAUnSetTCPKeepAlive(const CAEndpoint_t *endpoint);
477
478 /**
479  * Get the last error code (errno) to identify the reason
480  * when disconnection happens during keep-alive.
481  *
482  * @return             - Direct 'errno' value from kernel
483  */
484 int CAGetTCPLastErrorCode();
485
486 /**
487  * Set the interface to be used for outgoing TCP packets.
488  * Tcp client sockets (before establishing connection) will be bound to the IP address of the given WiFi interface.
489  * Internally, Iotivity reads the IP address assigned to the given interace
490  * and binds it with the TCP client socket before connect().
491  *
492  * @param[in]   ifname     WiFi interface name.
493  *
494  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
495  */
496 CAResult_t CASetTCPServerSocketBindIP(const char* ifname);
497 #endif
498
499 #ifdef __cplusplus
500 } /* extern "C" */
501 #endif
502
503 #endif /* CA_UTILS_INTERFACE_H_ */
504