tizen 2.3.1 release
[kernel/api/system-resource.git] / include / rd-network.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #ifndef __RD_NETWORK_H__
19 #define __RD_NETWORK_H__
20
21 #include <sys/types.h>
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26
27 /**
28  * @brief Enumeration for return type
29  */
30 typedef enum {
31         NETWORK_ERROR_NONMONITOR = -9,          /** < Process don't show watchdog popup */
32         NETWORK_ERROR_NOTIMPL = -7,              /**< Not implemented yet error */
33         NETWORK_ERROR_UNINITIALIZED = -6,        /**< Cgroup doen't mounted or daemon not started */
34         NETWORK_ERROR_NO_DATA = -5,              /**< Success, but no data */
35         NETWORK_ERROR_INVALID_PARAMETER = -4,/**< Invalid parameter */
36         NETWORK_ERROR_OUT_OF_MEMORY = -3,        /**< Out of memory */
37         NETWORK_ERROR_DB_FAILED = -2,    /**< Database error */
38         NETWORK_ERROR_FAIL = -1,                 /**< General error */
39         NETWORK_ERROR_NONE = 0           /**< General success */
40 } network_error_e;
41
42
43 /**
44  * @brief Enumeration for return type of the callback
45  */
46 typedef enum {
47         NETWORK_CANCEL = 0,                     /**< cancel */
48         NETWORK_CONTINUE = 1,           /**< continue */
49 } network_cb_ret_e;
50
51 /**
52  * @brief Enumeration for the monitored process state
53  */
54 typedef enum {
55         NETWORK_STATE_UNKNOWN = 0,
56         NETWORK_STATE_FOREGROUND = 1 << 1,              /** < foreground state */
57         NETWORK_STATE_BACKGROUND = 1 << 2,              /** < background state */
58         NETWORK_STATE_LAST_ELEM = 1 << 3
59 } network_state_e;
60
61 /**
62  * @brief Enumeration for network restriction state
63  */
64 typedef enum {
65         NETWORK_RESTRICTION_UNDEFINDED,
66         NETWORK_RESTRICTION_ACTIVATED,  /** < restriction activated - means it
67                                                 was sent to kernel */
68         NETWORK_RESTRICTION_EXCLUDED,   /** < restriction has been excluded -
69                                                 means it was sent to kernel as
70                                                 excluded */
71         NETWORK_RESTRICTION_REMOVED,     /** < restriction has been removed */
72
73         NETWORK_RESTRICTION_MAX_VALUE
74 } network_restriction_state;
75
76 /**
77  * @brief Enumeration for network interface types
78  */
79 typedef enum {
80         NETWORK_IFACE_UNKNOWN,  /**< undefined iface */
81         NETWORK_IFACE_DATACALL, /**< mobile data */
82         NETWORK_IFACE_WIFI,             /**< wifi data */
83         NETWORK_IFACE_WIRED,    /**< wired interface */
84         NETWORK_IFACE_BLUETOOTH,        /**< bluetooth interface */
85         NETWORK_IFACE_ALL,      /**< enumerate all network interface types */
86         NETWORK_IFACE_LAST_ELEM
87 } network_iface_e;
88
89 /**
90  * @brief Structure for time interval
91  * @details It's time interval. Zero interval since 0 til 0 means entires interval.
92  */
93 typedef struct {
94         time_t from;
95         time_t to;
96 } network_tm_interval_s;
97
98 /**
99  * @brief Enumeration for network connection period type
100  * @details Last received/sent mean counting data from the first connection of each interface
101  */
102 typedef enum {
103         NETWORK_CON_PERIOD_UNKNOWN,                     /**< Undefined period */
104         NETWORK_CON_PERIOD_LAST_RECEIVED_DATA,  /**< Last received data */
105         NETWORK_CON_PERIOD_LAST_SENT_DATA,              /**< Last sent data */
106         NETWORK_CON_PERIOD_TOTAL_RECEIVED_DATA, /**< Total received data */
107         NETWORK_CON_PERIOD_TOTAL_SENT_DATA,             /**< Total sent data */
108         NETWORK_CON_PERIOD_LAST_ELEM
109 } network_connection_period_e;
110
111 /**
112  * @brief Enumeration for network roaming type
113  */
114 typedef enum {
115         NETWORK_ROAMING_UNKNOWN,                /**< can't define roaming - roaming unknown */
116         NETWORK_ROAMING_ENABLE,         /**< in roaming */
117         NETWORK_ROAMING_DISABLE,                /**< not in roaming */
118         NETWORK_ROAMING_LAST_ELEM,
119 } network_roaming_e;
120
121 /**
122  * @brief Enumeration for hardware network protocol types
123  */
124 typedef enum {
125         NETWORK_PROTOCOL_NONE,                  /**< Network unknown */
126         NETWORK_PROTOCOL_DATACALL_NOSVC,                /**< Network no service */
127         NETWORK_PROTOCOL_DATACALL_EMERGENCY,            /**< Network emergency */
128         NETWORK_PROTOCOL_DATACALL_SEARCH,               /**< Network search 1900 */
129         NETWORK_PROTOCOL_DATACALL_2G,                   /**< Network 2G */
130         NETWORK_PROTOCOL_DATACALL_2_5G,         /**< Network 2.5G */
131         NETWORK_PROTOCOL_DATACALL_2_5G_EDGE,            /**< Network EDGE */
132         NETWORK_PROTOCOL_DATACALL_3G,                   /**< Network UMTS */
133         NETWORK_PROTOCOL_DATACALL_HSDPA,                /**< Network HSDPA */
134         NETWORK_PROTOCOL_DATACALL_LTE,          /**< Network LTE */
135         NETWORK_PROTOCOL_MAX_ELEM
136 } network_hw_net_protocol_e;
137
138 /**
139  * @brief Enumeration for the boolean option
140  * @details Description of the boolean option for enabling/disabling
141  *      network interfaces and enabling/disabling some behaviar
142  */
143 typedef enum {
144         NETWORK_OPTION_UNDEF,
145         NETWORK_OPTION_ENABLE,
146         NETWORK_OPTION_DISABLE
147 } network_option_e;
148
149 /**
150  * @brief Structure for network option
151  * @details Set of the options.
152  * version - contains structure version
153  * wifi - enable/disable wifi, NETWORK_OPTION_UNDEF to leave option as is
154  * datacall - enable/disable datacall, NETWORK_OPTION_UNDEF to leave option as is
155  * network_timer - set period of the updating data from the kernel,
156  *      0 to leave option as is
157  * datacall_logging - enable/disable datacall_logging,
158  *      NETWORK_OPTION_UNDEF to leave option as is
159  */
160 typedef struct {
161         unsigned char version;
162         network_option_e wifi;
163         network_option_e datacall;
164         time_t network_timer;
165         network_option_e datacall_logging;
166 } network_option_s;
167
168 /**
169  * @brief Set options, daemon will handle option setting.
170  * @param[in] options The network state option
171  *
172  * @return 0 on success, otherwise a negative error value
173  * @retval #NETWORK_ERROR_NONE Successful
174  * @retval #NETWORK_ERROR_FAIL General error
175  * @retval #NETWORK_ERROR_DB_FAILED Database error
176  * @retval #NETWORK_ERROR_OUT_OF_MEMORY Out of memory
177  * @retval #NETWORK_ERROR_INVALID_PARAMETER Invalid parameter
178  * @retval #NETWORK_ERROR_NO_DATA Success, but no data
179  * @retval #NETWORK_ERROR_UNINITIALIZED Cgroup doesn't mounted or daemon is not started
180  * @retval #NETWORK_ERROR_NOTIMPL No implemented yet error
181  * @retval #NETWORK_ERROR_NONMONITOR Process don't show watchdog popup
182  *
183  * @see #network_option_s
184  * @see #network_get_option
185  */
186 network_error_e network_set_option(const network_option_s *options);
187
188 /**
189  * @brief Get performance control options.
190  * @param[out] options The network state option
191  *
192  * @return 0 on success, otherwise a negative error value
193  * @retval #NETWORK_ERROR_NONE Successful
194  * @retval #NETWORK_ERROR_FAIL General error
195  * @retval #NETWORK_ERROR_DB_FAILED Database error
196  * @retval #NETWORK_ERROR_OUT_OF_MEMORY Out of memory
197  * @retval #NETWORK_ERROR_INVALID_PARAMETER Invalid parameter
198  * @retval #NETWORK_ERROR_NO_DATA Success, but no data
199  * @retval #NETWORK_ERROR_UNINITIALIZED Cgroup doesn't mounted or daemon is not started
200  * @retval #NETWORK_ERROR_NOTIMPL No implemented yet error
201  * @retval #NETWORK_ERROR_NONMONITOR Process don't show watchdog popup
202  *
203  * @see #network_option_s
204  * @see #network_set_option
205  */
206 network_error_e network_get_option(network_option_s *options);
207
208 /**
209  * @brief Make cgroup and put in it the given pid and generated classid
210  * @details If cgroup already exists function just put pid in it.
211  * @param[in] pid Process, that will be added to cgroup pkg name
212  * @param[in] pkg_name Package name
213  *
214  * @return 0 on success, otherwise a negative error value
215  * @retval #NETWORK_ERROR_NONE Successful
216  * @retval #NETWORK_ERROR_FAIL General error
217  * @retval #NETWORK_ERROR_DB_FAILED Database error
218  * @retval #NETWORK_ERROR_OUT_OF_MEMORY Out of memory
219  * @retval #NETWORK_ERROR_INVALID_PARAMETER Invalid parameter
220  * @retval #NETWORK_ERROR_NO_DATA Success, but no data
221  * @retval #NETWORK_ERROR_UNINITIALIZED Cgroup doesn't mounted or daemon is not started
222  * @retval #NETWORK_ERROR_NOTIMPL No implemented yet error
223  * @retval #NETWORK_ERROR_NONMONITOR Process don't show watchdog popup
224  *
225  * @see #network_get_classid_by_pkg_name
226  */
227 network_error_e network_make_cgroup_with_pid(const int pid,
228         const char *pkg_name);
229
230 /**
231  * @brief Get classid from cgroup with name pkg_name
232  * @param[in] pkg_name Name of the cgroup
233  * @param[in] create In case of true - create cgroup if it's not exists
234  * @return a positive value is classid, otherwise a negative error value
235  * @retval #NETWORK_ERROR_NONE Successful
236  * @retval #NETWORK_ERROR_FAIL General error
237  * @retval #NETWORK_ERROR_DB_FAILED Database error
238  * @retval #NETWORK_ERROR_OUT_OF_MEMORY Out of memory
239  * @retval #NETWORK_ERROR_INVALID_PARAMETER Invalid parameter
240  * @retval #NETWORK_ERROR_NO_DATA Success, but no data
241  * @retval #NETWORK_ERROR_UNINITIALIZED Cgroup doesn't mounted or daemon is not started
242  * @retval #NETWORK_ERROR_NOTIMPL No implemented yet error
243  * @retval #NETWORK_ERROR_NONMONITOR Process don't show watchdog popup
244  *
245  * @see #network_make_cgroup_with_pid
246  */
247 u_int32_t network_get_classid_by_pkg_name(const char *pkg_name, int create);
248
249 /**
250  * @brief Structure for network restriction information
251  * @details
252  * rs_type: foreground or background process
253  * iftype - interface type to apply restriction
254  * send_limit - amount number of engress bytes allowed for restriction
255  * rcv_limit - amount number of ingress bytes allowed for restriction
256  *              old behaviour for send_limit & rcv_limit was 0
257  * snd_warning_limit - threshold for warning notification on engress bytes
258  * rcv_warning_limit - threshold for warning notification on ingress bytes
259  *              value - WARNING_THRESHOLD_UNDEF means no threshold
260  *              this limit is different from quota warning threshold,
261  *              threshold means remaining, limit means occupaied
262  */
263 typedef struct {
264         network_state_e rs_type;
265         network_iface_e iftype;
266         int send_limit;
267         int rcv_limit;
268         int snd_warning_limit;
269         int rcv_warning_limit;
270 } network_restriction_s;
271
272 /**
273  * @brief Enumeration for restriction counter
274  */
275 typedef struct {
276         long incoming_bytes;
277         long outgoing_bytes;
278 } network_counter_s;
279
280 /**
281  * @brief Enumeration for holding data usage information
282  */
283 typedef struct {
284         network_counter_s cnt;
285         network_restriction_s rst;
286 } network_common_info;
287
288 /**
289  * @brief Set restriction information
290  * @details Set and apply restriction for application.
291  * It will create new restriction or modify existing.
292  * @param[in] app_id Application identifier, it's package name now
293  * @param[in] restriction Restriction to apply for application in foreground mode
294  * At least one of the restriction should be setted.
295  *
296  * @return 0 on success, otherwise a negative error value
297  * @retval #NETWORK_ERROR_NONE Successful
298  * @retval #NETWORK_ERROR_FAIL General error
299  * @retval #NETWORK_ERROR_DB_FAILED Database error
300  * @retval #NETWORK_ERROR_OUT_OF_MEMORY Out of memory
301  * @retval #NETWORK_ERROR_INVALID_PARAMETER Invalid parameter
302  * @retval #NETWORK_ERROR_NO_DATA Success, but no data
303  * @retval #NETWORK_ERROR_UNINITIALIZED Cgroup doesn't mounted or daemon is not started
304  * @retval #NETWORK_ERROR_NOTIMPL No implemented yet error
305  * @retval #NETWORK_ERROR_NONMONITOR Process don't show watchdog popup
306  *
307  * @see #network_restriction_s
308  * @see #network_remove_restriction
309  * @see #network_remove_restriction_by_iftype
310  */
311 network_error_e network_set_restriction(const char *app_id,
312                             const network_restriction_s *restriction);
313
314 /**
315  * @brief Structure for information on restrictions.
316  * @details
317  * app_id - application identification - copy it if you in
318  * callback function, don't store raw pointer on it
319  * iftype - type of network interface
320  */
321 typedef struct {
322         const char *app_id;
323         network_iface_e iftype;
324         network_restriction_state rst_state;
325         int rcv_limit;
326         int send_limit;
327 } network_restriction_info_s;
328
329 /**
330  * @brief callback for processing information of restrictions
331  */
332 typedef network_cb_ret_e(*network_restriction_cb)(
333         const network_restriction_info_s *info, void *user_data);
334
335 /**
336  * @brief Restrictions enumerate function
337  * @param[in] restriction_db The callback is called for each application that restrcited now
338  * @param[in] user_data User data will be passed to the callback function
339  *
340  * @return 0 on success, otherwise a negative error value
341  * @retval #NETWORK_ERROR_NONE Successful
342  * @retval #NETWORK_ERROR_FAIL General error
343  * @retval #NETWORK_ERROR_DB_FAILED Database error
344  * @retval #NETWORK_ERROR_OUT_OF_MEMORY Out of memory
345  * @retval #NETWORK_ERROR_INVALID_PARAMETER Invalid parameter
346  * @retval #NETWORK_ERROR_NO_DATA Success, but no data
347  * @retval #NETWORK_ERROR_UNINITIALIZED Cgroup doesn't mounted or daemon is not started
348  * @retval #NETWORK_ERROR_NOTIMPL No implemented yet error
349  * @retval #NETWORK_ERROR_NONMONITOR Process don't show watchdog popup
350  *
351  * @see #network_restriction_cb
352  */
353 network_error_e network_restriction_foreach(network_restriction_cb restriction_cb,
354                                 void *user_data);
355
356 /**
357  * @brief Remove existing restriction for application
358  * @details Remove existing restriction for application
359  *   It will delete restriction rule in kernel
360  * @param[in] app_id Application identifier, it's package name
361  *
362  * @return 0 on success, otherwise a negative error value
363  * @retval #NETWORK_ERROR_NONE Successful
364  * @retval #NETWORK_ERROR_FAIL General error
365  * @retval #NETWORK_ERROR_DB_FAILED Database error
366  * @retval #NETWORK_ERROR_OUT_OF_MEMORY Out of memory
367  * @retval #NETWORK_ERROR_INVALID_PARAMETER Invalid parameter
368  * @retval #NETWORK_ERROR_NO_DATA Success, but no data
369  * @retval #NETWORK_ERROR_UNINITIALIZED Cgroup doesn't mounted or daemon is not started
370  * @retval #NETWORK_ERROR_NOTIMPL No implemented yet error
371  * @retval #NETWORK_ERROR_NONMONITOR Process don't show watchdog popup
372  *
373  * @see #network_set_restriction
374  * @see #network_remove_restriction_by_iftype
375  */
376 network_error_e network_remove_restriction(const char *app_id);
377
378 /**
379  * @brief Remove existing restriction for application from interface type
380  * @details Remove existing restriction for application
381  *   It will delete restriction rule in kernel
382  * @param[in] app_id Application identifier, it's package name
383  * @param[in] iftype Interface type
384  *
385  * @return 0 on success, otherwise a negative error value
386  * @retval #NETWORK_ERROR_NONE Successful
387  * @retval #NETWORK_ERROR_FAIL General error
388  * @retval #NETWORK_ERROR_DB_FAILED Database error
389  * @retval #NETWORK_ERROR_OUT_OF_MEMORY Out of memory
390  * @retval #NETWORK_ERROR_INVALID_PARAMETER Invalid parameter
391  * @retval #NETWORK_ERROR_NO_DATA Success, but no data
392  * @retval #NETWORK_ERROR_UNINITIALIZED Cgroup doesn't mounted or daemon is not started
393  * @retval #NETWORK_ERROR_NOTIMPL No implemented yet error
394  * @retval #NETWORK_ERROR_NONMONITOR Process don't show watchdog popup
395  *
396  * @see #network_iface_e
397  * @see #network_set_restriction
398  * @see #network_remove_restriction_by_iftype
399  */
400 network_error_e network_remove_restriction_by_iftype(const char *app_id,
401                                              const network_iface_e iftype);
402
403 /**
404  * @brief Exclude restriction for application
405  * @details Exclude restriction for application
406  *   It will exclude restriction rule in kernel
407  * @param[in] app_id Application identifier, it's package name
408  *
409  * @return 0 on success, otherwise a negative error value
410  * @retval #NETWORK_ERROR_OK Successful
411  * @retval #NETWORK_ERROR_FAIL General error
412  * @retval #NETWORK_ERROR_DB_FAILED Database error
413  * @retval #NETWORK_ERROR_OUT_OF_MEMORY Out of memory
414  * @retval #NETWORK_ERROR_INVALID_PARAMETER Invalid parameter
415  * @retval #NETWORK_ERROR_NO_DATA Success, but no data
416  * @retval #NETWORK_ERROR_UNINITIALIZED Cgroup doesn't mounted or daemon is not started
417  * @retval #NETWORK_ERROR_NOTIMPL No implemented yet error
418  * @retval #NETWORK_ERROR_NONMONITOR Process don't show watchdog popup
419  *
420  * @see #network_set_restriction
421  * @see #network_exclude_restriction_by_iftype
422  */
423 network_error_e network_exclude_restriction(const char *app_id);
424
425 /**
426  * @brief Exclude restriction for application from interface type
427  * @details Exclude restriction for application
428  *   It will exclude restriction rule in kernel
429  * @param[in] app_id Application identifier, it's package name
430  * @param[in] iftype Interface type
431  *
432  * @return 0 on success, otherwise a negative error value
433  * @retval #NETWORK_ERROR_OK Successful
434  * @retval #NETWORK_ERROR_FAIL General error
435  * @retval #NETWORK_ERROR_DB_FAILED Database error
436  * @retval #NETWORK_ERROR_OUT_OF_MEMORY Out of memory
437  * @retval #NETWORK_ERROR_INVALID_PARAMETER Invalid parameter
438  * @retval #NETWORK_ERROR_NO_DATA Success, but no data
439  * @retval #NETWORK_ERROR_UNINITIALIZED Cgroup doesn't mounted or daemon is not started
440  * @retval #NETWORK_ERROR_NOTIMPL No implemented yet error
441  * @retval #NETWORK_ERROR_NONMONITOR Process don't show watchdog popup
442  *
443  * @see #network_iface_e
444  * @see #network_set_restriction
445  * @see #network_exclude_restriction_by_iftype
446  */
447 network_error_e network_exclude_restriction_by_iftype(
448         const char *app_id, const network_iface_e iftype);
449
450 /**
451  * @brief Structure for network activity information
452  */
453 typedef struct {
454         int type;               /*<< ingress/egress */
455         char *appid;
456         int iftype;
457         int bytes;
458 } network_activity_info_s;
459
460 /**
461  * @brief callback for network activity information of packet
462  */
463 typedef network_cb_ret_e(*network_activity_cb)(network_activity_info_s *info);
464
465 /**
466  * @brief Register activity callback
467  * @details This function registering callback which invokes per every packet.
468  * Function creates new reading thread and returns.
469  * @param[in] activity_cb Invoked per every packet with NET_ACTIVITY channel
470  *
471  * @return 0 on success, otherwise a negative error value
472  * @retval #NETWORK_ERROR_NONE Successful
473  * @retval #NETWORK_ERROR_FAIL General error
474  * @retval #NETWORK_ERROR_DB_FAILED Database error
475  * @retval #NETWORK_ERROR_OUT_OF_MEMORY Out of memory
476  * @retval #NETWORK_ERROR_INVALID_PARAMETER Invalid parameter
477  * @retval #NETWORK_ERROR_NO_DATA Success, but no data
478  * @retval #NETWORK_ERROR_UNINITIALIZED Cgroup doesn't mounted or daemon is not started
479  * @retval #NETWORK_ERROR_NOTIMPL No implemented yet error
480  * @retval #NETWORK_ERROR_NONMONITOR Process don't show watchdog popup
481  *
482  * @see #network_activity_cb
483  */
484 network_error_e network_register_activity_cb(network_activity_cb activity_cb);
485
486 /**
487  * @brief After invoking this function, application will be in the monitored scope
488  * @details It creates an appropriate cgroup,
489  * it generates classid for the network performance control.
490  * It creates a unit file for the systemd.
491  * @param[in] app_id Application identifier, it's package name now
492  * @param[in] pid Pid to put in to cgroup, or self pid of 0
493  *
494  * @return 0 on success, otherwise a negative error value
495  * @retval #NETWORK_ERROR_NONE Successful
496  * @retval #NETWORK_ERROR_FAIL General error
497  * @retval #NETWORK_ERROR_DB_FAILED Database error
498  * @retval #NETWORK_ERROR_OUT_OF_MEMORY Out of memory
499  * @retval #NETWORK_ERROR_INVALID_PARAMETER Invalid parameter
500  * @retval #NETWORK_ERROR_NO_DATA Success, but no data
501  * @retval #NETWORK_ERROR_UNINITIALIZED Cgroup doesn't mounted or daemon is not started
502  * @retval #NETWORK_ERROR_NOTIMPL No implemented yet error
503  * @retval #NETWORK_ERROR_NONMONITOR Process don't show watchdog popup
504  */
505 network_error_e network_join_app_performance(const char *app_id, const pid_t pid);
506
507 /**
508  * @brief Update the resourced counters and stores it in the database.
509  * @details Updates going asynchronyusly, it mean client can't be sure was
510  * counters updated or not after this function finished.
511  * To handle finish of the update process client need to
512  * regist callback function
513  * @see network_register_update_cb.
514  * Next counters updating will procced according to resourced
515  * update period, unless another network_update_statisitcs is
516  * not invoked.
517  *
518  * @return 0 on success, otherwise a negative error value
519  * @retval #NETWORK_ERROR_NONE Successful
520  * @retval #NETWORK_ERROR_FAIL General error
521  * @retval #NETWORK_ERROR_DB_FAILED Database error
522  * @retval #NETWORK_ERROR_OUT_OF_MEMORY Out of memory
523  * @retval #NETWORK_ERROR_INVALID_PARAMETER Invalid parameter
524  * @retval #NETWORK_ERROR_NO_DATA Success, but no data
525  * @retval #NETWORK_ERROR_UNINITIALIZED Cgroup doesn't mounted or daemon is not started
526  * @retval #NETWORK_ERROR_NOTIMPL No implemented yet error
527  * @retval #NETWORK_ERROR_NONMONITOR Process don't show watchdog popup
528  */
529 network_error_e network_update_statistics(void);
530
531 /*
532  * @brief Counters update information
533  * @details This structure is needed to prevent client API from modification
534  * in case of any information about update will be required.
535  */
536 struct network_update_info {
537         /*dummy content*/
538 };
539
540 /**
541  * @brief Callback for update counters
542  */
543 typedef network_cb_ret_e(*network_update_cb)(
544         const struct network_update_info *info,
545         void *user_data);
546
547 /**
548  * @brief Register callback for update counters.
549  * @details Callback function will be called if
550  * network_update_statistics is requested.
551  * To stop callbacks invocation return NETWORK_CANCEL from
552  * callback function or call @see network_unregister_update_cb.
553  *
554  * @param[in] user_data pointer to any data given to callback function.
555  *      Memory area should not be released until callback is unregistered.
556  *
557  * @return 0 on success, otherwise a negative error value
558  * @retval #NETWORK_ERROR_NONE Successful
559  * @retval #NETWORK_ERROR_FAIL General error
560  * @retval #NETWORK_ERROR_DB_FAILED Database error
561  * @retval #NETWORK_ERROR_OUT_OF_MEMORY Out of memory
562  * @retval #NETWORK_ERROR_INVALID_PARAMETER Invalid parameter
563  * @retval #NETWORK_ERROR_NO_DATA Success, but no data
564  * @retval #NETWORK_ERROR_UNINITIALIZED Cgroup doesn't mounted or daemon is not started
565  * @retval #NETWORK_ERROR_NOTIMPL No implemented yet error
566  * @retval #NETWORK_ERROR_NONMONITOR Process don't show watchdog popup
567  *
568  * @code
569
570 #include <stdio.h>
571 #include <stdlib.h>
572 #include <string.h>
573
574 #include <rd-network.h>
575
576 network_cb_ret_e network_update(const struct network_update_info *info,
577         void *user_data)
578 {
579         char buf[50];
580         char *str_data = user_data;
581         printf("Callback updated. Stop yes/no?: ");
582         scanf("%s", buf);
583         if (strcmp(buf, "yes") == 0)
584                 return NETWORK_CANCEL;
585
586         printf("user data is %s\n", user_data);
587
588         return NETWORK_CONTINUE;
589 }
590
591 int main(void)
592 {
593         network_error_e ret;
594
595         ecore_init();
596
597         char *user_data = (char *)malloc(1024);
598
599         strncpy(user_data, "hello", 1023);
600
601         ret = network_register_update_cb(network_update, (void *)user_data);
602
603         network_update_statistics();
604
605         ecore_main_loop_begin();
606
607         free(user_data);
608         ecore_shutdown();
609 }
610
611  * @endcode
612  */
613 network_error_e network_register_update_cb(network_update_cb update_cb,
614         void *user_data);
615
616 /**
617  * @brief Unregister update callback.
618  *
619  * @return 0 on success, otherwise a negative error value
620  * @retval #NETWORK_ERROR_NONE Successful
621  * @retval #NETWORK_ERROR_FAIL General error
622  * @retval #NETWORK_ERROR_DB_FAILED Database error
623  * @retval #NETWORK_ERROR_OUT_OF_MEMORY Out of memory
624  * @retval #NETWORK_ERROR_INVALID_PARAMETER Invalid parameter
625  * @retval #NETWORK_ERROR_NO_DATA Success, but no data
626  * @retval #NETWORK_ERROR_UNINITIALIZED Cgroup doesn't mounted or daemon is not started
627  * @retval #NETWORK_ERROR_NOTIMPL No implemented yet error
628  * @retval #NETWORK_ERROR_NONMONITOR Process don't show watchdog popup
629  */
630 void network_unregister_update_cb(void);
631
632 /**
633  * @brief Structure for selection rule applied
634  */
635 typedef struct {
636         unsigned char version;
637         time_t from;
638         time_t to;
639         network_iface_e iftype;
640         int granularity;
641 } network_selection_rule_s;
642
643 /**
644  * @brief Bundle structure for bringing all together application identification and properties
645  * @details
646  * app_id - application identification - copy it if you in
647  *   callback function, don't store raw pointer on it
648  * iface - interface name, NULL means all interfaces,
649  *   don't store raw pointer on it in the callback function, copy it by value
650  * interval - time interval for given result, NULL means entire interval
651  * foreground - foreground restrictions and counters
652  * background - background restrictions and counters
653  */
654 typedef struct {
655         const char *app_id;
656         network_iface_e iftype;
657         network_tm_interval_s *interval;
658         network_common_info foreground;
659         network_common_info background;
660         network_roaming_e roaming;
661         network_hw_net_protocol_e hw_net_protocol_type;
662 } network_info_s;
663
664 /**
665  * @brief Callback for enumerate counters and restrictions
666  */
667 typedef network_cb_ret_e(*network_info_cb) (const network_info_s *info,
668                                                void *user_data);
669
670 /**
671  * @brief Data usage enumerate function
672  * @details The callback is called for each application that used network
673  * in between timestamps specified.
674  * If interface name is not specified, each application will only appear
675  * once with the total traffic used over all interfaces.
676  * @param[in] rule Selection rule
677  * @param[in] info_cb The callback is called for each application
678  * that used network in between timestamps specified
679  * @param[in] user_data User data will be passed to the callback function
680  *
681  * @return 0 on success, otherwise a negative error value
682  * @retval #NETWORK_ERROR_NONE Successful
683  * @retval #NETWORK_ERROR_FAIL General error
684  * @retval #NETWORK_ERROR_DB_FAILED Database error
685  * @retval #NETWORK_ERROR_OUT_OF_MEMORY Out of memory
686  * @retval #NETWORK_ERROR_INVALID_PARAMETER Invalid parameter
687  * @retval #NETWORK_ERROR_NO_DATA Success, but no data
688  * @retval #NETWORK_ERROR_UNINITIALIZED Cgroup doesn't mounted or daemon is not started
689  * @retval #NETWORK_ERROR_NOTIMPL No implemented yet error
690  * @retval #NETWORK_ERROR_NONMONITOR Process don't show watchdog popup
691  *
692  * @see #network_selection_rule_s
693  * @see #network_info_cb
694  * @see #network_details_foreach
695  */
696 network_error_e network_foreach(const network_selection_rule_s *rule,
697                              network_info_cb info_cb, void *user_data);
698
699 /**
700  * @brief Data usage details enumerate function
701  * @detail
702  * If interface name is specified in rule, the callback will be called
703  * exactly 1 time with the total traffic counts for that interface
704  * by specified application in the specified time interval.
705  * If interface name is not specified, the callback will be called once
706  * for each interface used by application during the specified interval.
707  * It could be 0 if the application did not use any network interfaces
708  * during that period.
709  * @param[in] app_id Application id
710  * @param[in] rule Selection rule
711  * @param[in] info_cb The callback is called for each application
712  * that used network in between timestamps specified
713  * @param[in] user_data User data will be passed to the callback function
714  *
715  * @return 0 on success, otherwise a negative error value
716  * @retval #NETWORK_ERROR_NONE Successful
717  * @retval #NETWORK_ERROR_FAIL General error
718  * @retval #NETWORK_ERROR_DB_FAILED Database error
719  * @retval #NETWORK_ERROR_OUT_OF_MEMORY Out of memory
720  * @retval #NETWORK_ERROR_INVALID_PARAMETER Invalid parameter
721  * @retval #NETWORK_ERROR_NO_DATA Success, but no data
722  * @retval #NETWORK_ERROR_UNINITIALIZED Cgroup doesn't mounted or daemon is not started
723  * @retval #NETWORK_ERROR_NOTIMPL No implemented yet error
724  * @retval #NETWORK_ERROR_NONMONITOR Process don't show watchdog popup
725  *
726  * @see #network_selection_rule_s
727  * @see #network_info_cb
728  * @s22 #network_foreach
729  */
730 network_error_e network_details_foreach(const char *app_id,
731                                            network_selection_rule_s *rule,
732                                            network_info_cb info_cb,
733                                            void *user_data);
734
735 /**
736  * @brief Structure for reset rule
737  * @details It's statistics erasing description.
738  * app_id - Erase statistics per appropriate app_id.
739  * app_id can be NULL in this case erasing all datas
740  * iftype - Erase statistics per appropriate network interface type
741  * #network_iface_e, if iftype is NETWORK_IFACE_LAST_ELEM - erase all
742  * NETWORK_IFACE_UNKNOW - means undetermined interface on moment of storing data.
743  * interval - It's time interval, @see network_tm_interval_s.
744  * It should be set. Zero interval since 0 till 0 means entire interval.
745  * connection_state - It's mask on time interval.
746  * Possible variation LAST and TOTAL for send and received data.
747  */
748 typedef struct {
749         unsigned char version;
750         char *app_id;
751         network_iface_e iftype;
752         network_tm_interval_s *interval;
753         network_connection_period_e connection_state;
754 } network_reset_rule_s;
755
756 /**
757  * @brief Reset data usage information
758  * @param[in] rule Reset rule. It's statistics erasing description
759  *
760  * @return 0 on success, otherwise a negative error value
761  * @retval #NETWORK_ERROR_NONE Successful
762  * @retval #NETWORK_ERROR_FAIL General error
763  * @retval #NETWORK_ERROR_DB_FAILED Database error
764  * @retval #NETWORK_ERROR_OUT_OF_MEMORY Out of memory
765  * @retval #NETWORK_ERROR_INVALID_PARAMETER Invalid parameter
766  * @retval #NETWORK_ERROR_NO_DATA Success, but no data
767  * @retval #NETWORK_ERROR_UNINITIALIZED Cgroup doesn't mounted or daemon is not started
768  * @retval #NETWORK_ERROR_NOTIMPL No implemented yet error
769  * @retval #NETWORK_ERROR_NONMONITOR Process don't show watchdog popup
770  *
771  * @see #network_reset_rule_s
772  */
773 network_error_e network_reset(const network_reset_rule_s *rule);
774
775 /**
776  * @brief Reset filter for quota
777  * @details
778  * app_id is mandatory field
779  * iftype interface type, NETWORK_IFACE_UNKNOWN
780  * interface is not valid parameter, use NETWORK_IFACE_ALL instead
781  * roaming_type roaming type
782  * If user will not specify last 2 fields (UNKNOWN by default),
783  *   neither quota with defined interface nor
784  *   quota with defined roaming state will be removed.
785  */
786 typedef struct {
787         const char *app_id;
788         network_iface_e iftype;
789         network_roaming_e roaming;
790 } network_quota_reset_rule_s;
791
792 /**
793  * @brief Remove datausage quota by quota rule
794  * @param[in] rule reset filter for quota
795  *
796  * @return 0 on success, otherwise a negative error value
797  * @retval #NETWORK_ERROR_NONE Successful
798  * @retval #NETWORK_ERROR_FAIL General error
799  * @retval #NETWORK_ERROR_DB_FAILED Database error
800  * @retval #NETWORK_ERROR_OUT_OF_MEMORY Out of memory
801  * @retval #NETWORK_ERROR_INVALID_PARAMETER Invalid parameter
802  * @retval #NETWORK_ERROR_NO_DATA Success, but no data
803  * @retval #NETWORK_ERROR_UNINITIALIZED Cgroup doesn't mounted or daemon is not started
804  * @retval #NETWORK_ERROR_NOTIMPL No implemented yet error
805  * @retval #NETWORK_ERROR_NONMONITOR Process don't show watchdog popup
806  *
807  * @see #network_quota_reset_rule_s
808  * @see #network_set_quota
809  * @see #network_remove_quota_by_iftype
810  */
811 network_error_e network_remove_quota(
812         const network_quota_reset_rule_s *rule);
813
814 /**
815  * @brief Remove datausage quota by quota rule
816  * @param[in] app_id Application id
817  * @param[in] iftype Interface type
818  *
819  * @return 0 on success, otherwise a negative error value
820  * @retval #NETWORK_ERROR_NONE Successful
821  * @retval #NETWORK_ERROR_FAIL General error
822  * @retval #NETWORK_ERROR_DB_FAILED Database error
823  * @retval #NETWORK_ERROR_OUT_OF_MEMORY Out of memory
824  * @retval #NETWORK_ERROR_INVALID_PARAMETER Invalid parameter
825  * @retval #NETWORK_ERROR_NO_DATA Success, but no data
826  * @retval #NETWORK_ERROR_UNINITIALIZED Cgroup doesn't mounted or daemon is not started
827  * @retval #NETWORK_ERROR_NOTIMPL No implemented yet error
828  * @retval #NETWORK_ERROR_NONMONITOR Process don't show watchdog popup
829  *
830  * @see #network_quota_reset_rule_s
831  * @see #network_set_quota
832  * @see #network_remove_quota
833  */
834 network_error_e network_remove_quota_by_iftype(
835         const char *app_id, const network_iface_e iftype);
836
837 /**
838  * @brief Datausage quota
839  * @details
840  * time_period - time interval for quota, use predefined quota
841  * #network_quota_s_period_t
842  * snd_quota - quota for outcoming data
843  * rcv_quota - quota for incoming data
844  * warning_send_threshold - threshold for warning notification on engress bytes
845  * warning_rcv_threshold - threshold for warning notification on ingress bytes
846  * value - WARNING_THRESHOLD_UNDEF means no threshold
847  *       - WARNING_THRESHOLD_DEFAULT means system-resource will be
848  *         responsible for evaluation threshold value
849  * The threshold value is amount of bytes remaining till blocking
850  * quota_type - at present it can be foreground quota or background
851  * iftype - network interface type
852  * start_time - quota processing activation time, if NULL current time is used
853  */
854 typedef struct {
855         int time_period;
856         int64_t snd_quota;
857         int64_t rcv_quota;
858         int snd_warning_threshold;
859         int rcv_warning_threshold;
860         network_state_e quota_type;
861         network_iface_e iftype;
862         time_t *start_time;
863         network_roaming_e roaming_type;
864 } network_quota_s;
865
866 /**
867  * @brief Set datausage quota
868  * @details This function will set time interval based quota for data usage
869  * Restriction will be applied in case of exceeding of the quota
870  * during time interval
871  * @param app_id[in] Application identifier, it's package name
872  * @param quotas[in] Time interval based restriction for data usage
873  *
874  * @return 0 on success, otherwise a negative error value
875  * @retval #NETWORK_ERROR_NONE Successful
876  * @retval #NETWORK_ERROR_FAIL General error
877  * @retval #NETWORK_ERROR_DB_FAILED Database error
878  * @retval #NETWORK_ERROR_OUT_OF_MEMORY Out of memory
879  * @retval #NETWORK_ERROR_INVALID_PARAMETER Invalid parameter
880  * @retval #NETWORK_ERROR_NO_DATA Success, but no data
881  * @retval #NETWORK_ERROR_UNINITIALIZED Cgroup doesn't mounted or daemon is not started
882  * @retval #NETWORK_ERROR_NOTIMPL No implemented yet error
883  * @retval #NETWORK_ERROR_NONMONITOR Process don't show watchdog popup
884  *
885  * @see #network_set_quota
886  * @see #network_remove_quota
887  * @see #network_remove_quota_by_iftype
888  */
889 network_error_e network_set_quota(const char *app_id,
890                               const network_quota_s *quota);
891
892
893 /**
894  *
895  * This function get restriction state.
896  * State can be following:
897  *      NETWORK_RESTRICTION_UNDEFINDED - means restriction wasn't set
898  *      NETWORK_RESTRICTION_ACTIVATED  - means  restriction activated
899  *      NETWORK_RESTRICTION_EXCLUDED   - restriction has been excluded
900  *
901  * @code
902  * #include <rd-network.h>
903  *
904  * int is_whole_network_restricted()
905  * {
906  *      network_restriction_state state;
907  *      network_error_r ret_code = network_get_restriction_state(
908  *              RESOURCED_ALL_APP, NETWORK_IFACE_ALL, &state);
909  *      if (ret_code != NETWORK_ERROR_NONE &&
910  *              state == NETWORK_RESTRICTION_ACTIVATED)
911  *              return 1;
912  *      return 0;
913  * }
914  *
915  * @endcode
916  *
917  * @retval #NETWORK_ERROR_OK Successful
918  * @retval #NETWORK_ERROR_FAIL General error
919  * @retval #NETWORK_ERROR_DB_FAILED Database error
920  * @retval #NETWORK_ERROR_INVALID_PARAMETER Invalid parameter
921  *
922  * @see #network_iface_e
923  * @see #network_restriction_state
924  * @see #network_set_restriction
925  * @see #network_exclude_restriction_by_iftype
926  *
927  */
928 network_error_e network_get_restriction_state(const char *pkg_id,
929         network_iface_e iftype, network_restriction_state *state);
930
931
932 #ifdef __cplusplus
933 }
934 #endif
935
936 #endif  // __RD_NETWORK_H__