Modify flora license version.
[platform/core/messaging/msg-service.git] / utils / MsgDrmWrapper.cpp
1 /*
2 * Copyright 2012-2013  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 #include "MsgDebug.h"
18 #include "MsgDrmWrapper.h"
19 #include "MsgUtilFile.h"
20
21 #include <assert.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <dirent.h>
25 #include <string.h>
26 #include <sys/vfs.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <drm_client_types.h>
34 #include <drm_client.h>
35
36 #define MSG_MAX_DRM_FILE_PATH MSG_FILEPATH_LEN_MAX
37
38 bool MsgDrmRegisterFile(MSG_DRM_OPENMODE eMode, char *pBuffer, int nSize)
39 {
40         if (eMode == MSG_MODE_STREAM) {
41                 MSG_DEBUG("Fail(eMode == MSG_MODE_STREAM)");
42                 return false;
43         }
44
45         if (pBuffer == NULL) {
46                 MSG_DEBUG("[Error] pBuffer is NULL");
47                 return false;
48         }
49
50         MSG_DEBUG("buffer = %s, nSize = %d", pBuffer, nSize);
51
52         drm_bool_type_e isDrm;
53         int eDRMResult = drm_is_drm_file(pBuffer, &isDrm);
54
55         if (eDRMResult != DRM_RETURN_SUCCESS || isDrm != DRM_TRUE) {
56                 MSG_DEBUG("file is not drm file");
57                 return false;
58         }
59
60         drm_request_type_e request_type = DRM_REQUEST_TYPE_REGISTER_FILE;
61
62         eDRMResult = drm_process_request(request_type, pBuffer, NULL);
63         if (DRM_RETURN_SUCCESS != eDRMResult) {
64                 MSG_DEBUG("drm_process_request is failed : %d", eDRMResult);
65                 return false;
66         }
67         MSG_END();
68         return true;
69 }
70
71 bool MsgDrmUnregisterFile(char *szFilename)
72 {
73         if (szFilename == NULL) {
74                 MSG_DEBUG("[Error] szFilename is NULL");
75                 return false;
76         }
77
78         MSG_DEBUG("szFilename = %s", szFilename);
79
80         drm_request_type_e request_type = DRM_REQUEST_TYPE_UNREGISTER_FILE;
81
82         int eDRMResult = drm_process_request(request_type, szFilename, NULL); // Unregister a DCF file
83         if (DRM_RETURN_SUCCESS != eDRMResult) {
84                 MSG_DEBUG("drm_process_request : %d", eDRMResult);
85                 return false;
86         }
87
88         return true;
89 }
90
91 bool MsgDrmIsDrmFile(const char *szFilePath)
92 {
93         drm_bool_type_e isDrm;
94         int eDRMResult = drm_is_drm_file(szFilePath, &isDrm);
95
96         if (eDRMResult != DRM_RETURN_SUCCESS || isDrm != DRM_TRUE) {
97                 MSG_DEBUG("file is not drm file");
98                 return false;
99         }
100
101         return true;
102 }
103
104 /*Added to convert the .dm files in to .dcf files since our platform supports only .dcf :: Start*/
105 bool MsgDrmConvertDmtoDcfType(char *inputFile, char *outputFile)
106 {
107         return true;
108 }
109
110 bool MsgDrmGetDrmType(const char *szFileName, MSG_DRM_TYPE *eDRMType)
111 {
112         if (szFileName == NULL || eDRMType == NULL) {
113                 MSG_DEBUG("Param is NULL");
114                 return false;
115         }
116
117         drm_file_type_e file_type;
118         int result = drm_get_file_type(szFileName, &file_type);
119         if (result != DRM_RETURN_SUCCESS) {
120                 MSG_DEBUG("drm_get_file_type is failed %d", result);
121                 return false;
122         }
123
124         if (file_type == DRM_TYPE_OMA_V1) {
125                 drm_file_info_s drmInfo;
126                 bzero(&drmInfo, sizeof(drm_file_info_s));
127                 int eDRMResult = drm_get_file_info(szFileName, &drmInfo);
128                 if (DRM_RETURN_SUCCESS != eDRMResult) {
129                         MSG_DEBUG("drm_get_file_info is Fail eDRMResult = %d", eDRMResult);
130                         return false;
131                 }
132
133                 // Convert DRM_METHOD into MSG_DRM_TYPE
134                 switch (drmInfo.oma_info.method) {
135                 case DRM_METHOD_TYPE_FORWARD_LOCK:
136                         *eDRMType = MSG_DRM_FORWARD_LOCK;
137                         break;
138                 case DRM_METHOD_TYPE_COMBINED_DELIVERY:
139                         *eDRMType = MSG_DRM_COMBINED_DELIVERY;
140                         break;
141                 case DRM_METHOD_TYPE_SEPARATE_DELIVERY:
142                         *eDRMType = MSG_DRM_SEPARATE_DELIVERY;
143                         break;
144                 default:
145                         *eDRMType = MSG_DRM_NONE;
146                         break;
147                 }
148                 MSG_DEBUG("eDRMType : %d", *eDRMType);
149         } else {
150                 MSG_DEBUG("This is not a DRM_TYPE_OMA_V1 type");
151                 return false;
152         }
153
154         return true;
155 }
156
157 bool MsgDrmGetMimeTypeEx(const char *szFileName, char *szMimeType, int nMimeTypeLen)
158 {
159         if (!szFileName || !szMimeType || !nMimeTypeLen) {
160                 MSG_DEBUG("param is NULL");
161                 return false;
162         }
163
164         char strTemp[MSG_MAX_DRM_FILE_PATH + 1] = {0,};
165
166         strncpy(strTemp, szFileName, strlen(szFileName));
167
168         drm_content_info_s tdcfContentinfo;
169         memset(&tdcfContentinfo, 0x00, sizeof(drm_content_info_s));
170         int eDRMResult = drm_get_content_info(strTemp, &tdcfContentinfo); // Get attribute of DRM File
171         if (DRM_RETURN_SUCCESS == eDRMResult) {
172                 MSG_DEBUG("contentType = %s", tdcfContentinfo.mime_type);
173                 snprintf(szMimeType, nMimeTypeLen, "%s", tdcfContentinfo.mime_type);
174
175         } else {
176                 MSG_DEBUG("drm_get_content_info is failed %d", eDRMResult);
177                 return false;
178         }
179
180         return true;
181 }
182
183 bool MsgDrmGetContentID(const char *szFileName, char *szContentID, int nContentIDLen)
184 {
185         if (!szFileName || !szContentID || !nContentIDLen) {
186                 MSG_DEBUG("param is NULL");
187                 return false;
188         }
189
190         char strTemp[MSG_MAX_DRM_FILE_PATH + 1] = {0,};
191
192         strncpy(strTemp, szFileName, sizeof(strTemp));
193
194         drm_content_info_s  content_info;
195         memset(&content_info, 0x00, sizeof(drm_content_info_s));
196
197         int result = drm_get_content_info(strTemp, &content_info);
198         if (DRM_RETURN_SUCCESS == result) {
199                 MSG_DEBUG("contentID = %s", content_info.content_id);
200         snprintf(szContentID, nContentIDLen, "%s", content_info.content_id);
201         } else {
202                 MSG_DEBUG("drm_get_content_info is failed %d", result);
203                 return false;
204         }
205
206         return true;
207 }
208