3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the License);
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
22 #include <sys/types.h>
23 #include <sys/socket.h>
28 #include "haptic_module_log.h"
30 #define HAPTIC_RETRY_READ_COUNT 5
31 #define HAPTIC_PARAM_CNT 3
32 #define HAPTIC_MAXSTR 100
33 #define HAPTIC_MAXARG 16
34 #define PREDEF_HAPTIC "haptic"
35 #define SYSNOTI_SOCKET_PATH "/tmp/sn"
48 char *argv[HAPTIC_MAXARG];
51 static inline int __send_int(int fd, int val)
53 return write(fd, &val, sizeof(int));
56 static inline int __send_str(int fd, char *str)
63 r = write(fd, &len, sizeof(int));
66 if (len > HAPTIC_MAXSTR)
68 r = write(fd, &len, sizeof(int));
69 r = write(fd, str, len);
75 static int __sysnoti_send(struct sysnoti_type *msg)
79 struct sockaddr_un addr;
85 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
87 MODULE_ERROR("socket create failed");
91 bzero(&addr, sizeof(addr));
92 addr.sun_family = AF_UNIX;
93 strncpy(addr.sun_path, SYSNOTI_SOCKET_PATH, sizeof(addr.sun_path) - 1);
96 if (connect(sockfd, (struct sockaddr *)&addr, len) < 0) {
97 MODULE_ERROR("connect failed");
101 MODULE_LOG("connect : %x", sockfd);
103 __send_int(sockfd, msg->pid);
104 __send_int(sockfd, msg->cmd);
105 __send_str(sockfd, msg->type);
106 __send_str(sockfd, msg->path);
107 __send_int(sockfd, msg->argc);
108 for (i = 0; i < msg->argc; i++)
109 __send_str(sockfd, msg->argv[i]);
113 while ((r = read(sockfd, &ret, sizeof(int))) < 0) {
115 if (errno != EINTR) {
116 MODULE_LOG("read fail : %s(%d)", strerror(errno), errno);
121 if (retry_cnt == HAPTIC_RETRY_READ_COUNT) {
122 MODULE_ERROR("retry(%d) fail", retry_cnt);
127 MODULE_ERROR("Re-read for error(EINTR)");
131 MODULE_LOG("close (ret : %d) : %x", ret, sockfd);
136 int __haptic_call_predef_action(const char *type, int num, ...)
138 struct sysnoti_type msg;
143 if (type == NULL || num > HAPTIC_MAXARG) {
149 msg.cmd = CALL_HAPTIC_ACTION;
150 msg.type = (char *)type;
154 va_start(argptr, num);
155 for (i = 0; i < num; i++) {
156 args = va_arg(argptr, char *);
161 return __sysnoti_send(&msg);