summaryrefslogtreecommitdiffstats
path: root/src/SBServices.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/SBServices.c')
-rw-r--r--src/SBServices.c139
1 files changed, 109 insertions, 30 deletions
diff --git a/src/SBServices.c b/src/SBServices.c
index 1296245..69c7425 100644
--- a/src/SBServices.c
+++ b/src/SBServices.c
@@ -26,8 +26,8 @@
26#include <plist/plist.h> 26#include <plist/plist.h>
27 27
28#include "SBServices.h" 28#include "SBServices.h"
29#include "iphone.h" 29#include "property_list_service.h"
30#include "utils.h" 30#include "debug.h"
31 31
32/** Locks an sbservices client, done for thread safety stuff. 32/** Locks an sbservices client, done for thread safety stuff.
33 * 33 *
@@ -35,7 +35,7 @@
35 */ 35 */
36static void sbs_lock(sbservices_client_t client) 36static void sbs_lock(sbservices_client_t client)
37{ 37{
38 log_debug_msg("SBServices: Locked\n"); 38 debug_info("SBServices: Locked");
39 g_mutex_lock(client->mutex); 39 g_mutex_lock(client->mutex);
40} 40}
41 41
@@ -45,11 +45,48 @@ static void sbs_lock(sbservices_client_t client)
45 */ 45 */
46static void sbs_unlock(sbservices_client_t client) 46static void sbs_unlock(sbservices_client_t client)
47{ 47{
48 log_debug_msg("SBServices: Unlocked\n"); 48 debug_info("SBServices: Unlocked");
49 g_mutex_unlock(client->mutex); 49 g_mutex_unlock(client->mutex);
50} 50}
51 51
52sbservices_error_t sbservices_client_new(iphone_device_t device, int dst_port, sbservices_client_t *client) 52/**
53 * Convert a property_list_service_error_t value to a sbservices_error_t value.
54 * Used internally to get correct error codes.
55 *
56 * @param err A property_list_service_error_t error code
57 *
58 * @return A matching sbservices_error_t error code,
59 * SBSERVICES_E_UNKNOWN_ERROR otherwise.
60 */
61static sbservices_error_t sbservices_error(property_list_service_error_t err)
62{
63 switch (err) {
64 case PROPERTY_LIST_SERVICE_E_SUCCESS:
65 return SBSERVICES_E_SUCCESS;
66 case PROPERTY_LIST_SERVICE_E_INVALID_ARG:
67 return SBSERVICES_E_INVALID_ARG;
68 case PROPERTY_LIST_SERVICE_E_PLIST_ERROR:
69 return SBSERVICES_E_PLIST_ERROR;
70 case PROPERTY_LIST_SERVICE_E_MUX_ERROR:
71 return SBSERVICES_E_CONN_FAILED;
72 default:
73 break;
74 }
75 return SBSERVICES_E_UNKNOWN_ERROR;
76}
77
78/**
79 * Creates a new sbservices client.
80 *
81 * @param device The device to connect to.
82 * @param port The port on device to connect to.
83 * @param client Pointer that will point to a newly allocated
84 * sbservices_client_t upon successful return.
85 *
86 * @return SBSERVICES_E_SUCCESS on success, SBSERVICES_E_INVALID_ARG when
87 * client is NULL, or an SBSERVICES_E_* error code otherwise.
88 */
89sbservices_error_t sbservices_client_new(iphone_device_t device, uint16_t port, sbservices_client_t *client)
53{ 90{
54 /* makes sure thread environment is available */ 91 /* makes sure thread environment is available */
55 if (!g_thread_supported()) 92 if (!g_thread_supported())
@@ -58,38 +95,56 @@ sbservices_error_t sbservices_client_new(iphone_device_t device, int dst_port, s
58 if (!device) 95 if (!device)
59 return SBSERVICES_E_INVALID_ARG; 96 return SBSERVICES_E_INVALID_ARG;
60 97
61 /* Attempt connection */ 98 property_list_service_client_t plistclient = NULL;
62 iphone_connection_t connection = NULL; 99 sbservices_error_t err = sbservices_error(property_list_service_client_new(device, port, &plistclient));
63 if (iphone_device_connect(device, dst_port, &connection) != IPHONE_E_SUCCESS) { 100 if (err != SBSERVICES_E_SUCCESS) {
64 return SBSERVICES_E_CONN_FAILED; 101 return err;
65 } 102 }
66 103
67 sbservices_client_t client_loc = (sbservices_client_t) malloc(sizeof(struct sbservices_client_int)); 104 sbservices_client_t client_loc = (sbservices_client_t) malloc(sizeof(struct sbservices_client_int));
68 client_loc->connection = connection; 105 client_loc->parent = plistclient;
69 client_loc->mutex = g_mutex_new(); 106 client_loc->mutex = g_mutex_new();
70 107
71 *client = client_loc; 108 *client = client_loc;
72 return SBSERVICES_E_SUCCESS; 109 return SBSERVICES_E_SUCCESS;
73} 110}
74 111
112/**
113 * Frees an sbservices client.
114 *
115 * @param client The sbservices client to free.
116 *
117 * @return SBSERVICES_E_SUCCESS on success, SBSERVICES_E_INVALID_ARG when
118 * client is NULL, or an SBSERVICES_E_* error code otherwise.
119 */
75sbservices_error_t sbservices_client_free(sbservices_client_t client) 120sbservices_error_t sbservices_client_free(sbservices_client_t client)
76{ 121{
77 if (!client) 122 if (!client)
78 return SBSERVICES_E_INVALID_ARG; 123 return SBSERVICES_E_INVALID_ARG;
79 124
80 iphone_device_disconnect(client->connection); 125 sbservices_error_t err = sbservices_error(property_list_service_client_free(client->parent));
81 client->connection = NULL; 126 client->parent = NULL;
82 if (client->mutex) { 127 if (client->mutex) {
83 g_mutex_free(client->mutex); 128 g_mutex_free(client->mutex);
84 } 129 }
85 free(client); 130 free(client);
86 131
87 return SBSERVICES_E_SUCCESS; 132 return err;
88} 133}
89 134
135/**
136 * Gets the icon state of the connected device.
137 *
138 * @param client The connected sbservices client to use.
139 * @param state Pointer that will point to a newly allocated plist containing
140 * the current icon state. It is up to the caller to free the memory.
141 *
142 * @return SBSERVICES_E_SUCCESS on success, SBSERVICES_E_INVALID_ARG when
143 * client or state is invalid, or an SBSERVICES_E_* error code otherwise.
144 */
90sbservices_error_t sbservices_get_icon_state(sbservices_client_t client, plist_t *state) 145sbservices_error_t sbservices_get_icon_state(sbservices_client_t client, plist_t *state)
91{ 146{
92 if (!client || !client->connection || !state) 147 if (!client || !client->parent || !state)
93 return SBSERVICES_E_INVALID_ARG; 148 return SBSERVICES_E_INVALID_ARG;
94 149
95 sbservices_error_t res = SBSERVICES_E_UNKNOWN_ERROR; 150 sbservices_error_t res = SBSERVICES_E_UNKNOWN_ERROR;
@@ -99,17 +154,17 @@ sbservices_error_t sbservices_get_icon_state(sbservices_client_t client, plist_t
99 154
100 sbs_lock(client); 155 sbs_lock(client);
101 156
102 if (iphone_device_send_binary_plist(client->connection, dict) != IPHONE_E_SUCCESS) { 157 res = sbservices_error(property_list_service_send_binary_plist(client->parent, dict));
103 log_debug_msg("%s: could not send plist\n", __func__); 158 if (res != SBSERVICES_E_SUCCESS) {
159 debug_info("could not send plist, error %d", res);
104 goto leave_unlock; 160 goto leave_unlock;
105 } 161 }
106 plist_free(dict); 162 plist_free(dict);
107 dict = NULL; 163 dict = NULL;
108 164
109 if (iphone_device_receive_plist(client->connection, state) == IPHONE_E_SUCCESS) { 165 res = sbservices_error(property_list_service_receive_plist(client->parent, state));
110 res = SBSERVICES_E_SUCCESS; 166 if (res != SBSERVICES_E_SUCCESS) {
111 } else { 167 debug_info("could not get icon state, error %d", res);
112 log_debug_msg("%s: could not get icon state!\n", __func__);
113 if (*state) { 168 if (*state) {
114 plist_free(*state); 169 plist_free(*state);
115 *state = NULL; 170 *state = NULL;
@@ -124,9 +179,18 @@ leave_unlock:
124 return res; 179 return res;
125} 180}
126 181
182/**
183 * Sets the icon state of the connected device.
184 *
185 * @param client The connected sbservices client to use.
186 * @param newstate A plist containing the new iconstate.
187 *
188 * @return SBSERVICES_E_SUCCESS on success, SBSERVICES_E_INVALID_ARG when
189 * client or newstate is NULL, or an SBSERVICES_E_* error code otherwise.
190 */
127sbservices_error_t sbservices_set_icon_state(sbservices_client_t client, plist_t newstate) 191sbservices_error_t sbservices_set_icon_state(sbservices_client_t client, plist_t newstate)
128{ 192{
129 if (!client || !client->connection || !newstate) 193 if (!client || !client->parent || !newstate)
130 return SBSERVICES_E_INVALID_ARG; 194 return SBSERVICES_E_INVALID_ARG;
131 195
132 sbservices_error_t res = SBSERVICES_E_UNKNOWN_ERROR; 196 sbservices_error_t res = SBSERVICES_E_UNKNOWN_ERROR;
@@ -137,13 +201,12 @@ sbservices_error_t sbservices_set_icon_state(sbservices_client_t client, plist_t
137 201
138 sbs_lock(client); 202 sbs_lock(client);
139 203
140 if (iphone_device_send_binary_plist(client->connection, dict) != IPHONE_E_SUCCESS) { 204 res = sbservices_error(property_list_service_send_binary_plist(client->parent, dict));
141 log_debug_msg("%s: could not send plist\n", __func__); 205 if (res != SBSERVICES_E_SUCCESS) {
142 goto leave_unlock; 206 debug_info("could not send plist, error %d", res);
143 } 207 }
144 // NO RESPONSE 208 // NO RESPONSE
145 209
146leave_unlock:
147 if (dict) { 210 if (dict) {
148 plist_free(dict); 211 plist_free(dict);
149 } 212 }
@@ -151,9 +214,24 @@ leave_unlock:
151 return res; 214 return res;
152} 215}
153 216
217/**
218 * Get the icon of the specified app as PNG data.
219 *
220 * @param client The connected sbservices client to use.
221 * @param bundleId The bundle identifier of the app to retrieve the icon for.
222 * @param pngdata Pointer that will point to a newly allocated buffer
223 * containing the PNG data upon successful return. It is up to the caller
224 * to free the memory.
225 * @param pngsize Pointer to a uint64_t that will be set to the size of the
226 * buffer pngdata points to upon successful return.
227 *
228 * @return SBSERVICES_E_SUCCESS on success, SBSERVICES_E_INVALID_ARG when
229 * client, bundleId, or pngdata are invalid, or an SBSERVICES_E_* error
230 * code otherwise.
231 */
154sbservices_error_t sbservices_get_icon_pngdata(sbservices_client_t client, const char *bundleId, char **pngdata, uint64_t *pngsize) 232sbservices_error_t sbservices_get_icon_pngdata(sbservices_client_t client, const char *bundleId, char **pngdata, uint64_t *pngsize)
155{ 233{
156 if (!client || !client->connection || !pngdata) 234 if (!client || !client->parent || !bundleId || !pngdata)
157 return SBSERVICES_E_INVALID_ARG; 235 return SBSERVICES_E_INVALID_ARG;
158 236
159 sbservices_error_t res = SBSERVICES_E_UNKNOWN_ERROR; 237 sbservices_error_t res = SBSERVICES_E_UNKNOWN_ERROR;
@@ -164,19 +242,20 @@ sbservices_error_t sbservices_get_icon_pngdata(sbservices_client_t client, const
164 242
165 sbs_lock(client); 243 sbs_lock(client);
166 244
167 if (iphone_device_send_binary_plist(client->connection, dict) != IPHONE_E_SUCCESS) { 245 res = sbservices_error(property_list_service_send_binary_plist(client->parent, dict));
168 log_debug_msg("%s: could not send plist\n", __func__); 246 if (res != SBSERVICES_E_SUCCESS) {
247 debug_info("could not send plist, error %d", res);
169 goto leave_unlock; 248 goto leave_unlock;
170 } 249 }
171 plist_free(dict); 250 plist_free(dict);
172 251
173 dict = NULL; 252 dict = NULL;
174 if (iphone_device_receive_plist(client->connection, &dict) == IPHONE_E_SUCCESS) { 253 res = sbservices_error(property_list_service_receive_plist(client->parent, &dict));
254 if (res == SBSERVICES_E_SUCCESS) {
175 plist_t node = plist_dict_get_item(dict, "pngData"); 255 plist_t node = plist_dict_get_item(dict, "pngData");
176 if (node) { 256 if (node) {
177 plist_get_data_val(node, pngdata, pngsize); 257 plist_get_data_val(node, pngdata, pngsize);
178 } 258 }
179 res = SBSERVICES_E_SUCCESS;
180 } 259 }
181 260
182leave_unlock: 261leave_unlock: