1 /*
2  * camapplicationinfo.h - CAM (EN50221) Application Info resource
3  * Copyright (C) 2007 Alessandro Decina
4  *
5  * Authors:
6  *   Alessandro Decina <alessandro.d@gmail.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 
24 #include <string.h>
25 
26 #include "camapplicationinfo.h"
27 
28 #define GST_CAT_DEFAULT cam_debug_cat
29 #define TAG_APPLICATION_INFO_ENQUIRY 0x9F8020
30 #define TAG_APPLICATION_INFO_REPLY 0x9F8021
31 #define TAG_APPLICATION_INFO_ENTER_MENU 0x9F8022
32 
33 static CamReturn session_request_impl (CamALApplication * application,
34     CamSLSession * session, CamSLResourceStatus * status);
35 static CamReturn open_impl (CamALApplication * application,
36     CamSLSession * session);
37 static CamReturn close_impl (CamALApplication * application,
38     CamSLSession * session);
39 static CamReturn data_impl (CamALApplication * application,
40     CamSLSession * session, guint tag, guint8 * buffer, guint length);
41 
42 CamApplicationInfo *
cam_application_info_new(void)43 cam_application_info_new (void)
44 {
45   CamApplicationInfo *info;
46   CamALApplication *application;
47 
48   info = g_new0 (CamApplicationInfo, 1);
49   application = CAM_AL_APPLICATION (info);
50   _cam_al_application_init (application);
51   application->resource_id = CAM_AL_APPLICATION_INFO_ID;
52   application->session_request = session_request_impl;
53   application->open = open_impl;
54   application->close = close_impl;
55   application->data = data_impl;
56 
57   return info;
58 }
59 
60 void
cam_application_info_destroy(CamApplicationInfo * info)61 cam_application_info_destroy (CamApplicationInfo * info)
62 {
63   _cam_al_application_destroy (CAM_AL_APPLICATION (info));
64   g_free (info);
65 }
66 
67 static CamReturn
send_simple(CamApplicationInfo * info,CamSLSession * session,guint tag)68 send_simple (CamApplicationInfo * info, CamSLSession * session, guint tag)
69 {
70   guint8 *buffer;
71   guint offset;
72   guint buffer_size;
73   CamReturn ret;
74 
75   cam_al_calc_buffer_size (CAM_AL_APPLICATION (info)->al, 0, &buffer_size,
76       &offset);
77   buffer = g_malloc (buffer_size);
78 
79   ret = cam_al_application_write (CAM_AL_APPLICATION (info), session,
80       tag, buffer, buffer_size, 0);
81 
82   g_free (buffer);
83 
84   return ret;
85 }
86 
87 static CamReturn
send_application_info_enquiry(CamApplicationInfo * info,CamSLSession * session)88 send_application_info_enquiry (CamApplicationInfo * info,
89     CamSLSession * session)
90 {
91   GST_DEBUG ("sending application info enquiry");
92   return send_simple (info, session, TAG_APPLICATION_INFO_ENQUIRY);
93 }
94 
95 static CamReturn
session_request_impl(CamALApplication * application,CamSLSession * session,CamSLResourceStatus * status)96 session_request_impl (CamALApplication * application,
97     CamSLSession * session, CamSLResourceStatus * status)
98 {
99   *status = CAM_SL_RESOURCE_STATUS_OPEN;
100 
101   return CAM_RETURN_OK;
102 }
103 
104 static CamReturn
open_impl(CamALApplication * application,CamSLSession * session)105 open_impl (CamALApplication * application, CamSLSession * session)
106 {
107   CamApplicationInfo *info = CAM_APPLICATION_INFO (application);
108 
109   return send_application_info_enquiry (info, session);
110 }
111 
112 static CamReturn
close_impl(CamALApplication * application,CamSLSession * session)113 close_impl (CamALApplication * application, CamSLSession * session)
114 {
115   return CAM_RETURN_OK;
116 }
117 
118 static CamReturn
handle_application_info_reply(CamApplicationInfo * info,CamSLSession * session,guint8 * buffer,guint length)119 handle_application_info_reply (CamApplicationInfo * info,
120     CamSLSession * session, guint8 * buffer, guint length)
121 {
122 #ifndef GST_DISABLE_GST_DEBUG
123   guint8 type;
124   guint8 menu_length;
125   gchar menu[256];
126 
127   type = buffer[0];
128   menu_length = buffer[5];
129   menu_length = MIN (menu_length, 255);
130   memcpy (menu, buffer + 6, menu_length);
131   menu[menu_length] = 0;
132 
133   GST_INFO ("application info reply, type: %d, menu: %s", type, menu);
134 #endif
135   return CAM_RETURN_OK;
136 }
137 
138 static CamReturn
data_impl(CamALApplication * application,CamSLSession * session,guint tag,guint8 * buffer,guint length)139 data_impl (CamALApplication * application, CamSLSession * session,
140     guint tag, guint8 * buffer, guint length)
141 {
142   CamReturn ret;
143   CamApplicationInfo *info = CAM_APPLICATION_INFO (application);
144 
145   switch (tag) {
146     case TAG_APPLICATION_INFO_REPLY:
147       ret = handle_application_info_reply (info, session, buffer, length);
148       break;
149     default:
150       g_return_val_if_reached (CAM_RETURN_ERROR);
151   }
152 
153   return ret;
154 }
155