Re-organized sub-directory by category
[staging/basesystem.git] / hal / deck_hal / src / deck_hal.cpp
1 /*
2  * @copyright Copyright (c) 2017-2020 TOYOTA MOTOR CORPORATION.
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 #include <fcntl.h>
17 #include <unistd.h>
18 #include <linux/cdrom.h>
19 #include <sys/ioctl.h>
20 #include <deck_hal.h>
21 #include <native_service/frameworkunified_types.h>
22 #include <native_service/frameworkunified_framework_if.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include "deck_hal_deckhallog.h"
26
27 #define DECKHAL_THREAD "DeckHal"
28 #define DISC_PATH "/dev/sr0"
29 // static variables
30 static char g_notify_name[NOTIFY_NAME_MAX_SIZE + 1] = {0};
31 static bool g_register_status = false;  // deck_hal register status
32
33 // functions
34 EFrameworkunifiedStatus DeckNotifyInfo(const DECK_MSG_DELIVERY &DeliveryData);
35
36 EFrameworkunifiedStatus StopDiscRotation() {
37     int fd;
38     int ret;
39     fd = open(DISC_PATH, O_RDONLY);
40     if (fd < 0) {
41         FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "open failed.");
42         return eFrameworkunifiedStatusFail;
43     }
44     ret = ioctl(fd, CDROMSTOP);
45     close(fd);
46     if (ret != 0) {
47         FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "ioctl failed.");
48         return eFrameworkunifiedStatusFail;
49     }
50     return eFrameworkunifiedStatusOK;
51 }
52
53 EFrameworkunifiedStatus EjectDisc(DeckKind kind, DeckEjectMode mode) {
54     int fd;
55     int ret;
56     fd = open(DISC_PATH, O_RDONLY);
57     if (fd < 0) {
58         FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "open failed.");
59         return eFrameworkunifiedStatusFail;
60     }
61     ret = ioctl(fd, CDROM_LOCKDOOR);
62     if (ret != 0) {
63         FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "unlock disc failed.");
64         close(fd);
65         return eFrameworkunifiedStatusFail;
66     }
67     ret = ioctl(fd, CDROMEJECT);
68     close(fd);
69     if (ret != 0) {
70         FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "eject disc failed.");
71         return eFrameworkunifiedStatusFail;
72     }
73     return eFrameworkunifiedStatusOK;
74 }
75
76 EFrameworkunifiedStatus DeckHalSysSend(HANDLE h_app, PCSTR name, const DECK_CMD* cmd, uint8_t req_id) {
77   EFrameworkunifiedStatus ret = eFrameworkunifiedStatusOK;
78
79   if (NULL == h_app || NULL == name || NULL == cmd) {
80     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Input param is NULL\n");
81     return eFrameworkunifiedStatusInvldParam;
82   }
83
84   if (NOTIFY_NAME_MAX_SIZE < strlen(name)) {
85     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Input name length is overlapped\n");
86     return eFrameworkunifiedStatusInvldParam;
87   }
88   // DeckHalSysSend aims to send command to micon,
89   // since micon is stub now,so DeckHalSysSend is stub either.
90
91   // deck_hal should notify chande mode status after received mode change info
92   // from micon,since now micon is stub,notify change mode status to vehicle
93   // immediately.
94   // the data notify to vehile is just a example.
95   DECK_MSG_DELIVERY data;
96   data.cmd.cmdhdr.lgadr_from = 0x44;  // vehile:LSDRV_ADR_DVD_P
97   data.cmd.cmdhdr.lgadr_to = 0xC8;  // vehicle:LSDRV_OPC_DRV_M_CH_A_DVD
98   data.cmd.cmdhdr.opc = 0xC8;  // vehicle:LSDRV_OPC_DRV_M_CH_A_DVD
99   DeckNotifyInfo(data);
100
101   return ret;
102 }
103
104 EFrameworkunifiedStatus DeckHalRegister(HANDLE h_app, PCSTR notify_name) {
105   if (NULL == h_app) {
106     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Input h_app is NULL\n");
107     return eFrameworkunifiedStatusInvldParam;
108   }
109
110   if (NULL == notify_name) {
111     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Input notify_name is NULL\n");
112     return eFrameworkunifiedStatusInvldParam;
113   }
114
115   if (strlen(notify_name) > NOTIFY_NAME_MAX_SIZE) {
116     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Input notify_name length is overlapped\n");
117     return eFrameworkunifiedStatusInvldParam;
118   }
119
120   strncpy(g_notify_name, notify_name, NOTIFY_NAME_MAX_SIZE);
121
122   // now micon is stub,in the future,should register to micon
123
124   g_register_status = true;
125
126   return eFrameworkunifiedStatusOK;
127 }
128
129 // DeckNotifyInfo aims to send deck infos to vehicle
130 // if micon is not stub in the future,after received meesage from micon
131 // or any other ways triggered by micon,deck_hal should call this function
132 // with different input data to send these data to vehicle.
133 EFrameworkunifiedStatus DeckNotifyInfo(const DECK_MSG_DELIVERY &DeliveryData) {
134   EFrameworkunifiedStatus ret = eFrameworkunifiedStatusOK;
135   HANDLE send_handle = NULL;
136
137   if (g_register_status == true) {
138     send_handle = McOpenSender(g_notify_name);
139     if (send_handle == NULL) {
140       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "FrameworkunifiedMcOpenSender to _CWORD121_Shadow Failed.");
141       return eFrameworkunifiedStatusFail;
142     }
143
144     ret = McSend(send_handle, DECKHAL_THREAD, CID_DECK_SYS_DELIVERY,
145                  sizeof(DECK_MSG_DELIVERY), &DeliveryData);
146
147     if (eFrameworkunifiedStatusOK != ret) {
148       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Failed to send CID_DECK_SYS_DELIVERY.");
149       ret = eFrameworkunifiedStatusFail;
150     }
151
152     if (send_handle != NULL) {
153       McClose(send_handle);
154       send_handle = NULL;
155     }
156   }
157
158   return ret;
159 }