diff options
| author | 2010-01-13 15:08:14 +0100 | |
|---|---|---|
| committer | 2010-01-13 15:08:14 +0100 | |
| commit | c9e2217059f561f87cf8b6af5067505f827c7297 (patch) | |
| tree | b3e46b6ed89035f9ad0ee696cf46c57445cabbf7 /src/sbservices.c | |
| parent | 0ea52d01b817e35e4d4fceb57c9267124e60dab3 (diff) | |
| download | libimobiledevice-c9e2217059f561f87cf8b6af5067505f827c7297.tar.gz libimobiledevice-c9e2217059f561f87cf8b6af5067505f827c7297.tar.bz2 | |
Rename service implementation sources to lowercase for consistency
Diffstat (limited to 'src/sbservices.c')
| -rw-r--r-- | src/sbservices.c | 269 |
1 files changed, 269 insertions, 0 deletions
diff --git a/src/sbservices.c b/src/sbservices.c new file mode 100644 index 0000000..2254c64 --- /dev/null +++ b/src/sbservices.c | |||
| @@ -0,0 +1,269 @@ | |||
| 1 | /* | ||
| 2 | * sbservices.c | ||
| 3 | * SpringBoard Services implementation. | ||
| 4 | * | ||
| 5 | * Copyright (c) 2009 Nikias Bassen, All Rights Reserved. | ||
| 6 | * | ||
| 7 | * This library is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * This library is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with this library; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include <string.h> | ||
| 23 | #include <stdlib.h> | ||
| 24 | #include <unistd.h> | ||
| 25 | #include <arpa/inet.h> | ||
| 26 | #include <plist/plist.h> | ||
| 27 | |||
| 28 | #include "sbservices.h" | ||
| 29 | #include "property_list_service.h" | ||
| 30 | #include "debug.h" | ||
| 31 | |||
| 32 | /** Locks an sbservices client, done for thread safety stuff. | ||
| 33 | * | ||
| 34 | * @param client The sbservices client to lock. | ||
| 35 | */ | ||
| 36 | static void sbs_lock(sbservices_client_t client) | ||
| 37 | { | ||
| 38 | debug_info("SBServices: Locked"); | ||
| 39 | g_mutex_lock(client->mutex); | ||
| 40 | } | ||
| 41 | |||
| 42 | /** Unlocks an sbservices client, done for thread safety stuff. | ||
| 43 | * | ||
| 44 | * @param client The sbservices client to unlock | ||
| 45 | */ | ||
| 46 | static void sbs_unlock(sbservices_client_t client) | ||
| 47 | { | ||
| 48 | debug_info("SBServices: Unlocked"); | ||
| 49 | g_mutex_unlock(client->mutex); | ||
| 50 | } | ||
| 51 | |||
| 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 | */ | ||
| 61 | static 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 | */ | ||
| 89 | sbservices_error_t sbservices_client_new(iphone_device_t device, uint16_t port, sbservices_client_t *client) | ||
| 90 | { | ||
| 91 | /* makes sure thread environment is available */ | ||
| 92 | if (!g_thread_supported()) | ||
| 93 | g_thread_init(NULL); | ||
| 94 | |||
| 95 | if (!device) | ||
| 96 | return SBSERVICES_E_INVALID_ARG; | ||
| 97 | |||
| 98 | property_list_service_client_t plistclient = NULL; | ||
| 99 | sbservices_error_t err = sbservices_error(property_list_service_client_new(device, port, &plistclient)); | ||
| 100 | if (err != SBSERVICES_E_SUCCESS) { | ||
| 101 | return err; | ||
| 102 | } | ||
| 103 | |||
| 104 | sbservices_client_t client_loc = (sbservices_client_t) malloc(sizeof(struct sbservices_client_int)); | ||
| 105 | client_loc->parent = plistclient; | ||
| 106 | client_loc->mutex = g_mutex_new(); | ||
| 107 | |||
| 108 | *client = client_loc; | ||
| 109 | return SBSERVICES_E_SUCCESS; | ||
| 110 | } | ||
| 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 | */ | ||
| 120 | sbservices_error_t sbservices_client_free(sbservices_client_t client) | ||
| 121 | { | ||
| 122 | if (!client) | ||
| 123 | return SBSERVICES_E_INVALID_ARG; | ||
| 124 | |||
| 125 | sbservices_error_t err = sbservices_error(property_list_service_client_free(client->parent)); | ||
| 126 | client->parent = NULL; | ||
| 127 | if (client->mutex) { | ||
| 128 | g_mutex_free(client->mutex); | ||
| 129 | } | ||
| 130 | free(client); | ||
| 131 | |||
| 132 | return err; | ||
| 133 | } | ||
| 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 | */ | ||
| 145 | sbservices_error_t sbservices_get_icon_state(sbservices_client_t client, plist_t *state) | ||
| 146 | { | ||
| 147 | if (!client || !client->parent || !state) | ||
| 148 | return SBSERVICES_E_INVALID_ARG; | ||
| 149 | |||
| 150 | sbservices_error_t res = SBSERVICES_E_UNKNOWN_ERROR; | ||
| 151 | |||
| 152 | plist_t dict = plist_new_dict(); | ||
| 153 | plist_dict_insert_item(dict, "command", plist_new_string("getIconState")); | ||
| 154 | |||
| 155 | sbs_lock(client); | ||
| 156 | |||
| 157 | res = sbservices_error(property_list_service_send_binary_plist(client->parent, dict)); | ||
| 158 | if (res != SBSERVICES_E_SUCCESS) { | ||
| 159 | debug_info("could not send plist, error %d", res); | ||
| 160 | goto leave_unlock; | ||
| 161 | } | ||
| 162 | plist_free(dict); | ||
| 163 | dict = NULL; | ||
| 164 | |||
| 165 | res = sbservices_error(property_list_service_receive_plist(client->parent, state)); | ||
| 166 | if (res != SBSERVICES_E_SUCCESS) { | ||
| 167 | debug_info("could not get icon state, error %d", res); | ||
| 168 | if (*state) { | ||
| 169 | plist_free(*state); | ||
| 170 | *state = NULL; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | leave_unlock: | ||
| 175 | if (dict) { | ||
| 176 | plist_free(dict); | ||
| 177 | } | ||
| 178 | sbs_unlock(client); | ||
| 179 | return res; | ||
| 180 | } | ||
| 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 | */ | ||
| 191 | sbservices_error_t sbservices_set_icon_state(sbservices_client_t client, plist_t newstate) | ||
| 192 | { | ||
| 193 | if (!client || !client->parent || !newstate) | ||
| 194 | return SBSERVICES_E_INVALID_ARG; | ||
| 195 | |||
| 196 | sbservices_error_t res = SBSERVICES_E_UNKNOWN_ERROR; | ||
| 197 | |||
| 198 | plist_t dict = plist_new_dict(); | ||
| 199 | plist_dict_insert_item(dict, "command", plist_new_string("setIconState")); | ||
| 200 | plist_dict_insert_item(dict, "iconState", plist_copy(newstate)); | ||
| 201 | |||
| 202 | sbs_lock(client); | ||
| 203 | |||
| 204 | res = sbservices_error(property_list_service_send_binary_plist(client->parent, dict)); | ||
| 205 | if (res != SBSERVICES_E_SUCCESS) { | ||
| 206 | debug_info("could not send plist, error %d", res); | ||
| 207 | } | ||
| 208 | // NO RESPONSE | ||
| 209 | |||
| 210 | if (dict) { | ||
| 211 | plist_free(dict); | ||
| 212 | } | ||
| 213 | sbs_unlock(client); | ||
| 214 | return res; | ||
| 215 | } | ||
| 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 | */ | ||
| 232 | sbservices_error_t sbservices_get_icon_pngdata(sbservices_client_t client, const char *bundleId, char **pngdata, uint64_t *pngsize) | ||
| 233 | { | ||
| 234 | if (!client || !client->parent || !bundleId || !pngdata) | ||
| 235 | return SBSERVICES_E_INVALID_ARG; | ||
| 236 | |||
| 237 | sbservices_error_t res = SBSERVICES_E_UNKNOWN_ERROR; | ||
| 238 | |||
| 239 | plist_t dict = plist_new_dict(); | ||
| 240 | plist_dict_insert_item(dict, "command", plist_new_string("getIconPNGData")); | ||
| 241 | plist_dict_insert_item(dict, "bundleId", plist_new_string(bundleId)); | ||
| 242 | |||
| 243 | sbs_lock(client); | ||
| 244 | |||
| 245 | res = sbservices_error(property_list_service_send_binary_plist(client->parent, dict)); | ||
| 246 | if (res != SBSERVICES_E_SUCCESS) { | ||
| 247 | debug_info("could not send plist, error %d", res); | ||
| 248 | goto leave_unlock; | ||
| 249 | } | ||
| 250 | plist_free(dict); | ||
| 251 | |||
| 252 | dict = NULL; | ||
| 253 | res = sbservices_error(property_list_service_receive_plist(client->parent, &dict)); | ||
| 254 | if (res == SBSERVICES_E_SUCCESS) { | ||
| 255 | plist_t node = plist_dict_get_item(dict, "pngData"); | ||
| 256 | if (node) { | ||
| 257 | plist_get_data_val(node, pngdata, pngsize); | ||
| 258 | } | ||
| 259 | } | ||
| 260 | |||
| 261 | leave_unlock: | ||
| 262 | if (dict) { | ||
| 263 | plist_free(dict); | ||
| 264 | } | ||
| 265 | sbs_unlock(client); | ||
| 266 | return res; | ||
| 267 | |||
| 268 | } | ||
| 269 | |||
