lib: tmonitor: Add get APIs for array type attributes
[platform/core/system/pass.git] / src / monitor / request-handler.c
1 /*
2  * PASS (Power Aware System Service)
3  *
4  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 /**
20  * @file        request-handler-thread.c
21  * @brief       TBD
22  * @ingroup     TBD
23  */
24
25 #include <glib.h>
26
27 #include <util/common.h>
28 #include <util/log.h>
29 #include <util/resource.h>
30 #include <util/thread.h>
31 #include <util/device-notifier.h>
32 #include <util/devices.h>
33 #include <util/request.h>
34 #include <util/request-handler.h>
35
36 #include <string.h>
37 #include <arpa/inet.h>
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <sys/time.h>
42 #include <assert.h>
43
44 static void free_resource(gpointer key, gpointer value, gpointer user_data)
45 {
46         struct resource *res = value;
47
48         delete_resource(res);
49 }
50
51 static void update_resource(gpointer key, gpointer value, gpointer user_data)
52 {
53         struct resource *res = value;
54         int ret;
55
56         ret = update_resource_attrs(res);
57         if (ret < 0)
58                 _E("failed to update resource attributes (name:%s,id:%d)\n",
59                                         res->name, res->id);
60 }
61
62 static void finalize_request_client(struct request_client *client)
63 {
64         if (!client)
65                 return;
66
67         if (client->resource_table)
68                 g_hash_table_foreach(client->resource_table, (GHFunc)free_resource, NULL);
69 }
70
71 static void
72 register_resource_to_client(struct request_client *client, struct resource *res)
73 {
74         g_hash_table_insert(client->resource_table, (gpointer)&res->id, (gpointer)res);
75 }
76
77 static struct resource *
78 get_resource_by_id(struct request_client *client, int resource_id)
79 {
80         struct resource *res;
81
82         res = g_hash_table_lookup(client->resource_table, (gpointer)&resource_id);
83
84         return res;
85 }
86
87 static int handle_request_create_resource(struct request_client *client, char *args)
88 {
89         struct resource *res;
90         int resource_type;
91
92         if (!client || !args)
93                 return -ENOENT;
94
95         /**
96          * Format of REQUEST_CREATE_RESOURCE args:
97          *  - <RESOURCE_TYPE>
98          */
99         resource_type = atoi(args);
100
101         res = create_resource(resource_type);
102         if (!res)
103                 return -EINVAL;
104
105         register_resource_to_client(client, res);
106
107         return res->id;
108 }
109
110 static int handle_request_delete_resource(struct request_client *client, char *args)
111 {
112         struct resource *res;
113         int resource_id;
114
115         if (!client || !args)
116                 return -ENOENT;
117
118         /**
119          * Format of REQUEST_DELETE_RESOURCE args:
120          *  - <RESOURCE_ID>
121          */
122         resource_id = atoi(args);
123
124         res = get_resource_by_id(client, resource_id);
125         if (!res)
126                 return -EINVAL;
127
128         delete_resource(res);
129
130         return 0;
131 }
132
133 static int handle_request_update_resource(struct request_client *client, char *args)
134 {
135         struct resource *res;
136         int resource_id;
137
138         if (!client || !args)
139                 return -ENOENT;
140
141         /**
142          * Format of REQUEST_UPDATE_RESOURCE args:
143          *  - <RESOURCE_ID>
144          */
145         resource_id = atoi(args);
146
147         res = get_resource_by_id(client, resource_id);
148         if (!res)
149                 return -EINVAL;
150
151         return update_resource_attrs(res);
152 }
153
154 static int handle_request_get_resource_count(struct request_client *client, char *args)
155 {
156         int resource_type;
157
158         if (!client || !args)
159                 return -ENOENT;
160
161         /**
162          * Format of REQUEST_GET_RESOURCE_COUNT args:
163          *  - <RESOURCE_TYPE>
164          */
165         resource_type = atoi(args);
166
167         return get_resource_device_count(resource_type);
168 }
169
170 static int handle_request_update_resource_all(struct request_client *client, char *args)
171 {
172         if (!client)
173                 return -ENOENT;
174
175         /**
176          * Format of REQUEST_UPDATE_RESOURCE args:
177          *  - NULL
178          */
179         if (client->resource_table)
180                 g_hash_table_foreach(client->resource_table, (GHFunc)update_resource, NULL);
181
182         return 0;
183 }
184
185 static int handle_request_set_resource_attr(struct request_client *client, char *args, int request_type)
186 {
187         struct resource *res;
188         int resource_id, ret;
189         u_int64_t interest_masks;
190
191         if (!client || !args)
192                 return -ENOENT;
193
194         /**
195          * Format of REQUEST_SET_RESOURCE_ATTR and REQUEST_UNSET_RESOURCE_ATTR args:
196          *  - <RESOURCE_ID:INTEREST_MASK>
197          */
198         if (sscanf(args, "%d:%"PRIu64, &resource_id, &interest_masks) < 2)
199                 return -EINVAL;
200
201         res = get_resource_by_id(client, resource_id);
202         if (!res)
203                 return -EINVAL;
204
205         switch (request_type) {
206         case REQUEST_SET_RESOURCE_ATTR:
207                 ret = set_resource_attr_interest(res, interest_masks);
208                 break;
209         case REQUEST_UNSET_RESOURCE_ATTR:
210                 ret = unset_resource_attr_interest(res, interest_masks);
211                 break;
212         default:
213                 return -EINVAL;
214         }
215
216         return ret;
217 }
218
219 static int handle_request_is_resource_attr_supported(struct request_client *client, char *args)
220 {
221         struct resource *res;
222         int resource_id;
223         u_int64_t attr_id;
224
225         if (!client || !args)
226                 return -ENOENT;
227
228         /**
229          * Format of REQUEST_IS_RESOURCE_ATTR_SUPPORTED args:
230          *  - <RESOURCE_ID:RESOURCE_ATTR_ID>
231          */
232         if (sscanf(args, "%d:%"PRIu64"", &resource_id, &attr_id) < 2)
233                 return -EINVAL;
234
235         res = get_resource_by_id(client, resource_id);
236         if (!res)
237                 return -EINVAL;
238
239         return (int)is_resource_attr_supported(res, attr_id);
240 }
241
242 static int handle_request_set_resource_ctrl(struct request_client *client, char *args)
243 {
244         struct resource *res;
245         int resource_id, value;
246         u_int64_t ctrl_id;
247
248         if (!client || !args)
249                 return -ENOENT;
250
251         /**
252          * Format of REQUEST_SET_RESOURCE_CTRL args:
253          *  - <RESOURCE_ID:CONTROL_ID:CONTROL_VALUE>
254          */
255         if (sscanf(args, "%d:%"PRIu64":%d", &resource_id, &ctrl_id, &value) < 3)
256                 return -EINVAL;
257
258         res = get_resource_by_id(client, resource_id);
259         if (!res)
260                 return -EINVAL;
261
262         return set_resource_control(res, ctrl_id, (void *)(intptr_t)value);
263 }
264
265 static int handle_request_get_value_int(struct request_client *client, char *args, int32_t *value)
266 {
267         struct resource *res;
268         int resource_id;
269         u_int64_t attr_id;
270
271         if (!client || !args)
272                 return -ENOENT;
273
274         /**
275          * Format of REQUEST_GET_VALUE_INT args:
276          *  - <RESOURCE_ID:ATTR_ID>
277          */
278         if (sscanf(args, "%d:%"PRIu64, &resource_id, &attr_id) < 2)
279                 return -EINVAL;
280
281         res = get_resource_by_id(client, resource_id);
282         if (!res)
283                 return -EINVAL;
284
285         return get_resource_attr_int(res, attr_id, value);
286 }
287
288 static int handle_request_get_value_int64(struct request_client *client, char *args, int64_t *value)
289 {
290         struct resource *res;
291         int resource_id;
292         u_int64_t attr_id;
293
294         if (!client || !args)
295                 return -ENOENT;
296
297         /**
298          * Format of REQUEST_GET_VALUE_INT64 args:
299          *  - <RESOURCE_ID:ATTR_ID>
300          */
301         if (sscanf(args, "%d:%"PRIu64, &resource_id, &attr_id) < 2)
302                 return -EINVAL;
303
304         res = get_resource_by_id(client, resource_id);
305         if (!res)
306                 return -EINVAL;
307
308         return get_resource_attr_int64(res, attr_id, value);
309 }
310
311 static int
312 handle_request_get_value_uint(struct request_client *client, char *args, u_int32_t *value)
313 {
314         struct resource *res;
315         int resource_id;
316         u_int64_t attr_id;
317
318         if (!client || !args)
319                 return -ENOENT;
320
321         /**
322          * Format of REQUEST_GET_VALUE_UINT args:
323          *  - <RESOURCE_ID:ATTR_ID>
324          */
325         if (sscanf(args, "%d:%"PRIu64, &resource_id, &attr_id) < 2)
326                 return -EINVAL;
327
328         res = get_resource_by_id(client, resource_id);
329         if (!res)
330                 return -EINVAL;
331
332         return get_resource_attr_uint(res, attr_id, value);
333 }
334
335 static int
336 handle_request_get_value_uint64(struct request_client *client, char *args, u_int64_t *value)
337 {
338         struct resource *res;
339         int resource_id;
340         u_int64_t attr_id;
341
342         if (!client || !args)
343                 return -ENOENT;
344
345         /**
346          * Format of REQUEST_GET_VALUE_UINT64 args:
347          *  - <RESOURCE_ID:ATTR_ID>
348          */
349         if (sscanf(args, "%d:%"PRIu64, &resource_id, &attr_id) < 2)
350                 return -EINVAL;
351
352         res = get_resource_by_id(client, resource_id);
353         if (!res)
354                 return -EINVAL;
355
356         return get_resource_attr_uint64(res, attr_id, value);
357 }
358
359 static int handle_request_get_value_double(struct request_client *client, char *args, double *value)
360 {
361         struct resource *res;
362         int resource_id;
363         u_int64_t attr_id;
364
365         if (!client || !args)
366                 return -ENOENT;
367
368         /**
369          * Format of REQUEST_GET_VALUE_DOUBLE args:
370          *  - <RESOURCE_ID:ATTR_ID>
371          */
372         if (sscanf(args, "%d:%"PRIu64, &resource_id, &attr_id) < 2)
373                 return -EINVAL;
374
375         res = get_resource_by_id(client, resource_id);
376         if (!res)
377                 return -EINVAL;
378
379         return get_resource_attr_double(res, attr_id, value);
380 }
381
382 static int handle_request_get_value_string(struct request_client *client, char *args, char *value)
383 {
384         struct resource *res;
385         int resource_id;
386         u_int64_t attr_id;
387
388         if (!client || !args)
389                 return -ENOENT;
390
391         /**
392          * Format of REQUEST_GET_VALUE_INT args:
393          *  - <RESOURCE_ID:ATTR_ID>
394          */
395         if (sscanf(args, "%d:%"PRIu64, &resource_id, &attr_id) < 2)
396                 return -EINVAL;
397
398         res = get_resource_by_id(client, resource_id);
399         if (!res)
400                 return -EINVAL;
401
402         return get_resource_attr_string(res, attr_id, value);
403 }
404
405 static int
406 handle_request_get_value_array(struct request_client *client, char *args, struct array_value **arr)
407 {
408         struct resource *res;
409         int resource_id;
410         u_int64_t attr_id;
411
412         if (!client || !args)
413                 return -ENOENT;
414
415         /**
416          * Format of REQUEST_GET_VALUE_ARRAY args:
417          *  - <RESOURCE_ID:ATTR_ID>
418          */
419         if (sscanf(args, "%d:%"PRIu64, &resource_id, &attr_id) < 2)
420                 return -EINVAL;
421
422         res = get_resource_by_id(client, resource_id);
423         if (!res)
424                 return -EINVAL;
425
426         return get_resource_attr_array(res, attr_id, arr);
427 }
428
429 static int split_request_type_and_args(char *buffer, char **args)
430 {
431         char *request_type_str;
432
433         request_type_str = strsep(&buffer, ":");
434
435         *args = buffer;
436
437         return atoi(request_type_str);
438 }
439
440 #define ADD_RESPONSE(__buf__, __remain__, __format__, ...)      { \
441         int __len__ = snprintf(__buf__, __remain__, __format__, __VA_ARGS__); \
442         __buf__ += __len__;  \
443         __remain__ -= __len__; \
444         if (__remain__ < 0) \
445                 _E("failed to add response"); \
446 } \
447
448 static void handle_request(struct request_client *client, char *buffer)
449 {
450         char _response[REQUEST_BUFFER_MAX];
451         char *response = _response;
452         char *args;
453         int request_type;
454         int ret;
455         int buffer_len = REQUEST_BUFFER_MAX;
456
457         request_type = split_request_type_and_args(buffer, &args);
458
459         /**
460          * Format of response
461          *  - <REQUEST_TYPE[:REQUEST_RESULT_PAYLOAD]:REQUEST_RESULT_VALUE>
462          */
463         ADD_RESPONSE(response, buffer_len, "%d:", request_type);
464
465         switch (request_type) {
466         case REQUEST_CREATE_RESOURCE:
467                 ret = handle_request_create_resource(client, args);
468                 if (ret < 0)
469                         _D("failed to create resource");
470                 break;
471         case REQUEST_DELETE_RESOURCE:
472                 ret = handle_request_delete_resource(client, args);
473                 if (ret < 0)
474                         _E("failed to delete resource");
475                 break;
476         case REQUEST_UPDATE_RESOURCE:
477                 ret = handle_request_update_resource(client, args);
478                 if (ret < 0)
479                         _D("failed to update resource");
480                 break;
481         case REQUEST_GET_RESOURCE_COUNT:
482                 ret = handle_request_get_resource_count(client, args);
483                 if (ret < 0)
484                         _E("failed to get resource count");
485                 break;
486         case REQUEST_UPDATE_RESOURCE_ALL:
487                 ret = handle_request_update_resource_all(client, args);
488                 if (ret < 0)
489                         _E("failed to update all resources");
490                 break;
491         case REQUEST_SET_RESOURCE_CTRL:
492                 ret = handle_request_set_resource_ctrl(client, args);
493                 if (ret < 0)
494                         _D("failed to set resource control");
495                 break;
496         case REQUEST_SET_RESOURCE_ATTR:
497         case REQUEST_UNSET_RESOURCE_ATTR:
498                 ret = handle_request_set_resource_attr(client, args, request_type);
499                 if (ret < 0)
500                         _E("failed to %s attr interest",
501                                 request_type == REQUEST_SET_RESOURCE_ATTR
502                                 ? "set" : "unset");
503                 break;
504         case REQUEST_IS_RESOURCE_ATTR_SUPPORTED:
505                 ret = handle_request_is_resource_attr_supported(client, args);
506                 if (ret < 0)
507                         _E("failed to check whether attr is supported or not");
508                 break;
509         case REQUEST_GET_VALUE_INT:
510                 {
511                         int32_t value;
512
513                         ret = handle_request_get_value_int(client, args, &value);
514                         if (ret < 0)
515                                 _D("failed to get value");
516
517                         ADD_RESPONSE(response, buffer_len, "%d:", value);
518                 }
519                 break;
520         case REQUEST_GET_VALUE_INT64:
521                 {
522                         int64_t value;
523
524                         ret = handle_request_get_value_int64(client, args, &value);
525                         if (ret < 0)
526                                 _D("failed to get value");
527
528                         ADD_RESPONSE(response, buffer_len, "%"PRId64":", value);
529                 }
530                 break;
531         case REQUEST_GET_VALUE_UINT:
532                 {
533                         u_int32_t value;
534
535                         ret = handle_request_get_value_uint(client, args, &value);
536                         if (ret < 0)
537                                 _D("failed to get value");
538
539                         ADD_RESPONSE(response, buffer_len, "%u:", value);
540                 }
541                 break;
542         case REQUEST_GET_VALUE_UINT64:
543                 {
544                         u_int64_t value;
545
546                         ret = handle_request_get_value_uint64(client, args, &value);
547                         if (ret < 0)
548                                 _D("failed to get value");
549
550                         ADD_RESPONSE(response, buffer_len, "%"PRIu64":", value);
551                 }
552                 break;
553         case REQUEST_GET_VALUE_DOUBLE:
554                 {
555                         double value;
556
557                         ret = handle_request_get_value_double(client, args, &value);
558                         if (ret < 0)
559                                 _D("failed to get value");
560
561                         ADD_RESPONSE(response, buffer_len, "%lf:", value);
562                 }
563                 break;
564         case REQUEST_GET_VALUE_STRING:
565                 {
566                         char value[BUFF_MAX];
567
568                         ret = handle_request_get_value_string(client, args, value);
569                         if (ret < 0)
570                                 _D("failed to get value");
571
572                         ADD_RESPONSE(response, buffer_len, "%s:", value);
573                 }
574                 break;
575         case REQUEST_GET_VALUE_ARRAY:
576                 {
577                         struct array_value *array;
578                         int i;
579
580                         ret = handle_request_get_value_array(client, args, &array);
581                         if (ret < 0)
582                                 _D("failed to get value");
583
584                         if (array->length == 0) {
585                                 ADD_RESPONSE(response, buffer_len, "%d|%d|:",
586                                                 array->type, array->length);
587                                 break;
588                         }
589
590                         ADD_RESPONSE(response, buffer_len, "%d|%d|", array->type, array->length);
591
592                         switch (array->type) {
593                         case DATA_TYPE_INT:
594                                 for (i = 0; i < array->length - 1; i++)
595                                         ADD_RESPONSE(response, buffer_len, "%d,",
596                                                         ((int32_t *)array->data)[i]);
597                                 ADD_RESPONSE(response, buffer_len, "%d:",
598                                                 ((int32_t *)array->data)[i]);
599                                 break;
600                         case DATA_TYPE_INT64:
601                                 for (i = 0; i < array->length - 1; i++)
602                                         ADD_RESPONSE(response, buffer_len, "%"PRId64",",
603                                                         ((int64_t *)array->data)[i]);
604                                 ADD_RESPONSE(response, buffer_len, "%"PRId64":",
605                                                 ((int64_t *)array->data)[i]);
606                                 break;
607                         case DATA_TYPE_UINT:
608                                 for (i = 0; i < array->length - 1; i++)
609                                         ADD_RESPONSE(response, buffer_len, "%u,",
610                                                         ((u_int32_t *)array->data)[i]);
611                                 ADD_RESPONSE(response, buffer_len, "%u:",
612                                                 ((u_int32_t *)array->data)[i]);
613                                 break;
614                         case DATA_TYPE_UINT64:
615                                 for (i = 0; i < array->length - 1; i++)
616                                         ADD_RESPONSE(response, buffer_len, "%"PRIu64",",
617                                                         ((u_int64_t *)array->data)[i]);
618                                 ADD_RESPONSE(response, buffer_len, "%"PRIu64":",
619                                                 ((u_int64_t *)array->data)[i]);
620                                 break;
621                         case DATA_TYPE_DOUBLE:
622                                 for (i = 0; i < array->length - 1; i++)
623                                         ADD_RESPONSE(response, buffer_len, "%lf,",
624                                                         ((double *)array->data)[i]);
625                                 ADD_RESPONSE(response, buffer_len, "%lf:",
626                                                 ((double *)array->data)[i]);
627                                 break;
628                         case DATA_TYPE_STRING:
629                                 for (i = 0; i < array->length - 1; i++)
630                                         ADD_RESPONSE(response, buffer_len, "%s,",
631                                                         ((char **)array->data)[i]);
632                                 ADD_RESPONSE(response, buffer_len, "%s:",
633                                                 ((char **)array->data)[i]);
634                                 break;
635                         default:
636                                 _E("Not supported data type");
637                         }
638                 }
639                 break;
640         default:
641                 _E("Invliad request type: %d", request_type);
642                 ret = -EINVAL;
643                 break;
644         }
645         ADD_RESPONSE(response, buffer_len, "%d", ret);
646
647         if (send(client->socket_fd, _response, strlen(_response), 0) < 0)
648                 _E("Failed to send respones, error: %s", strerror(errno));
649 }
650
651 static GList *g_request_client_head;
652 static GMutex g_server_lock;
653
654 static void add_client_to_list(struct request_client *client)
655 {
656         if (!client)
657                 return;
658
659         g_mutex_lock(&g_server_lock);
660         g_request_client_head =
661                         g_list_append(g_request_client_head, (gpointer)client);
662         g_mutex_unlock(&g_server_lock);
663 }
664
665 static void remove_client_from_list(struct request_client *client)
666 {
667         if (!client)
668                 return;
669
670         g_mutex_lock(&g_server_lock);
671         g_request_client_head =
672                         g_list_remove(g_request_client_head, (gpointer)client);
673         g_mutex_unlock(&g_server_lock);
674 }
675
676 static void destroy_request_client(struct request_client *client)
677 {
678         remove_client_from_list(client);
679         free(client);
680 }
681
682 static int request_handler_func(void *data, void **result)
683 {
684         char buffer[REQUEST_BUFFER_MAX + 1];
685         struct request_client *client = (struct request_client *)data;
686         int len;
687
688         _D("Start worker thread for client-%d", client->socket_fd);
689
690         while (1) {
691                 len = recv(client->socket_fd, buffer, REQUEST_BUFFER_MAX, 0);
692                 if (len == 0) {
693                         _D("Client-%d is disconnected", client->socket_fd);
694                         goto out;
695                 }
696
697                 if (len < 0) {
698                         _E("Error occurs while receiving request: %s", strerror(errno));
699                         goto out;
700                 }
701
702                 buffer[len] = '\0';
703                 handle_request(client, buffer);
704         }
705 out:
706         finalize_request_client(client);
707         close(client->socket_fd);
708         destroy_request_client(client);
709
710         return THREAD_RETURN_DONE;
711 }
712
713 int create_request_client(int socket_fd)
714 {
715         struct request_client *client;
716
717         client = malloc(sizeof(struct request_client));
718         if (!client) {
719                 _E("Failed to allocate memory of request_client");
720                 return -ENOMEM;
721         }
722
723         client->socket_fd = socket_fd;
724         client->resource_table = g_hash_table_new(g_int_hash, g_int_equal);
725
726         create_daemon_thread(&client->worker, request_handler_func, client);
727
728         add_client_to_list(client);
729
730         return 0;
731 }
732