Skip to content
Snippets Groups Projects
libmobile.h 10.5 KiB
Newer Older
  • Learn to ignore specific revisions
  • #ifndef LIBMOBILE_H
    #define LIBMOBILE_H
    #include <curl/curl.h>
    #include <json-c/json.h>
    #include <string.h>
    #include <libubox/blobmsg.h>
    
    #include <libubus.h>
    
    /***************************************************
     * Libmobile - A 4G Dongle library
     *
     * Libmobile provides a library to perform basic functionality for a 4G dongle through
     * the use of HTTP GET/POST.
     *
     * All library calls will prepare the GET or POST query, and send the request to the dongle.
     * The response from the server is parsed through a callback, <write_func>. The dongle's response
     * is then returned. For POST requests the dongle responds with {"result": "success"} on a
     * successful query execution, and with {"result": failure"} on an unsuccessful query execution,
     * note that an unsuccessful query execution can mean a variety of things such as: incorrect pin,
     * wrong input format, value already set, etc. For a GET request the variable's requested for with
     * their corresponding values are returned, important to note that even variables that are not
     * available within the system will be returned, but with no value set. The return string for all
     * calls is the servers response pointed to by a char pointer.
     *
     *
     * How to extend libmobile
     *
     * To extend this library add a new function with the corresponding documentation. The naming scheme
     * should follow the format <mobile_get_{variables}> for GET requests and <mobile_{action_performed}>
     * for POST requests. The structure of the calls are that the <mobile_...> functions prepare the query,
     * for GET this means providing the variables to request from the dongle, provided as an input to
     * <mobile_get_request>, for POST this means providing the POSTFIELDS argument query to the method
     * <mobile_post_request>. The dongle's response is parsed through a callback, a pointer is then returned
     * pointing to the response.
     *
     ***************************************************/
    
    /***************************************************
     * IMPORTANT NOTE
     *
     * The library calls will allocate memory for the dongle's response and return the pointer to this memory,
     * the caller is then responsible for freeing this memory by calling free(returned_pointer).
     ***************************************************/
    
    
    /**
     * Function: write_to_ubus
     *
     * Prints a json_object pointer's json structure to ubus.
     *
     * Parameters:
     * 		parsed_response - A struct json_object pointer to json structure to be printed to ubus.
     * 		ctx - Ubus context containing connection.
     * 		req - Information for from the ubus request.
     *
     * Returns:
     * 		0 On success.
     * 		-1 On failure.
     */
    
    int write_to_ubus(struct json_object *parsed_response, struct ubus_context *ctx, struct ubus_request_data *req);
    
    
    /**
     * Function: curl_cleaner
     *
     * Free's the curl environment.
     *
     * Parameters:
     *		curl - The curl easy handle variable.
     */
    
    void curl_cleaner(CURLcode *curl);
    
    
    /**
     * Function: write_func
     *
     * The callback function from performing the curl command, response from server will be parsed here.
     *
     * Parameters:
     *		buffer - Contains chunk of response from server.
     *		size - Size (byte) of type in buffer array.
     *		nmemb - Number of members in buffer.
     *		userp - Pointer to area allocated for storing results from server.
     *
     * Returns:
     * 		Number of bytes managed (size*nmemb).
     */
    
    size_t write_func(void *buffer, size_t size, size_t nmemb, void *userp);
    
    
    /**
     * Function: json_to_blob
     *
     * Parses a json_object pointer to a corresponding blob buffer.
     *
     * Parameters:
     * 		response - json_object pointer to be replicated in a blob buffer.
     * 		bb - The blob buffer to hold the results.
     *
     * Returns:
     *		Blob buffer containing the replicated json_object.
     */
    
    struct blob_buf json_to_blob(struct json_object *response, struct blob_buf bb);
    
    
    /**
     * Function: apn_profile_idx
     *
     * Finds the index of a given profile name from all available profiles.
     *
     * Parameters:
     *		apn_profiles - json_object pointer to apn_profiles (gotten from a previous call to <mobile_get_apn_profiles>)
     *		name - Name of the APN index for which will be searched.
     *
     * Returns:
     *		Index of the APN on success.
     *		-1 on failure.
     */
    
    
    int apn_profile_idx(struct json_object *apn_profiles, char *name);
    
    
    /**
     * Function: get_apn_profiles_len
     *
     * Finds the number of APN profiles available.
     *
     * Returns:
     *		Number of APN profiles available on success.
     *		-1 on failure.
     */
    
    int get_apn_profiles_len(void);
    
    /**
     * Function: mobile_connect_network
     *
     * Connects the dongle's network.
     *
     * Returns:
    
     *		A string containing the servers response (a json string containing {"result": "success"/"failure" }),
     	indicating whether the server successfully executed the query.
    
    char *mobile_connect_network(void);
    
    /**
     * Function: mobile_disconnect_network
     *
     * Disconnects the dongle's network.
     *
     * Returns:
    
     *		A string containing the servers response (a json string containing {"result": "success"/"failure" }),
     	indicating whether the server successfully executed the query.
    
    char *mobile_disconnect_network(void);
    
    
    /**
     * Function: mobile_delete_apn
     *
     * Deletes an existing APN profile from the APN list.
     *
     * Parameters:
     * 		name - The name of the APN profile to be removed.
     *
     * Returns:
    
     *		A string containing the servers response (a json string containing {"result": "success"/"failure" }),
     	indicating whether the server successfully executed the query.
    
     *		NULL on failure.
     */
    char *mobile_delete_apn(char *name);
    
    /**
     * Function: mobile_enable_roaming
     *
     * Enables the roaming option on the dongle.
     *
     * Returns:
    
     *		A string containing the servers response (a json string containing {"result": "success"/"failure" }),
     	indicating whether the server successfully executed the query.
    
    char *mobile_enable_roaming(void);
    
    
    /**
     * Function: mobile_disable_roaming
     *
     * Disables the roaming option on the dongle.
     *
     * Returns:
     *		A string containing the servers response (a json string containing {"result": "success"/"failure" }),
     	indicating whether the server successfully executed the query.
     *		NULL on failure.
     */
    
    char *mobile_disable_roaming(void);
    
    
    /**
     * Function: mobile_disable_roaming
     *
     * Gets the current roam status of the dongle.
     *
     * Returns:
     *		A string containing the servers response in JSON format, {"roam_setting_option": "on"/"off" }
     *		NULL on failure.
     */
    
    char *mobile_get_roam_status(void);
    
    
    /**
     * Function: mobile_get_wan_apn
     *
     * Gets the currently active APN profile's WAN name.
     *
     * Returns:
     *		A string containing the servers response in JSON format, {"wan_apn": "<name>" } on success.
     *		NULL on failure.
     */
    
    char *mobile_get_wan_apn(void);
    
    
    /**
     * Function: mobile_get_pinnumber
     *
     * Gets the pinnumber, indicating how many tries are remaining before the SIM card is locked.
     *
     * Returns:
     *		A string containing the servers response in JSON format, {"pinnumber": {1..3} } on success.
     *		NULL on failure.
     */
    
    char *mobile_get_pinnumber(void);
    
    
    /**
     * Function: mobile_get_pin_status
     *
     * Gets the pinnumber, indicating whether pin is enabled or not.
     *
     * Returns:
     *		A string containing the servers response in JSON format, {"pinnumber": {0..1} } on success.
     *		NULL on failure.
     */
    
    char *mobile_get_pin_status(void);
    
    
    /**
     * Function: mobile_get_modem_state
     *
     * Gets a variety of variables related to the modems state, i.e. network.
     *
     * Returns:
     *		A string containing the servers response in JSON format on success.
     *		NULL on failure.
     */
    
    char *mobile_get_modem_state(void);
    
    
    /**
     * Function: mobile_get_apn_profiles
     *
     * Gets all available APN profile names.
     *
     * Returns:
     *		A string containing the servers response in JSON format on success.
     *		NULL on failure.
     */
    
    char *mobile_get_apn_profiles(void);
    
    
    /**
     * Function: mobile_create_apn_profile
     *
     * Creates a new APN profile.
     *
     * Parameter:
     * 		name - Name of the APN (and WAN) profile to create.
     *
     * Returns:
     *		A string containing the servers response (a json string containing {"result": "success"/"failure" }),
     	indicating whether the server successfully executed the query.
     *		NULL on failure.
     */
    char *mobile_create_apn_profile(char *name);
    
    /**
     * Function: mobile_set_apn_profile
     *
     * Sets a name APN profile to be activate.
     *
     * Parameter:
     * 		name - Name of the APN profile to activate.
     *
     * Returns:
     *		A string containing the servers response (a json string containing {"result": "success"/"failure" }),
     	indicating whether the server successfully executed the query.
     *		NULL on failure.
     */
    char *mobile_set_apn_profile(char *name);
    
    /**
     * Function: mobile_enable_pin
     *
     * Enables pin on the SIM card.
     *
     * Parameter:
     * 		pin - The current pin card of the SIM.
     *
     * Returns:
     *		A string containing the servers response (a json string containing {"result": "success"/"failure" }),
     	indicating whether the server successfully executed the query.
     *		NULL on failure.
     */
    
    char *mobile_enable_pin(char *pin);
    
    
    /**
     * Function: mobile_set_pin
     *
     * Sets a new pin for the SIM card.
     *
     * Parameter:
     * 		current_pin - The currently active pin for the SIM card.
     * 		new_pin - The pin code which to set as the active pin for the SIM card.
     *
     * Returns:
     *		A string containing the servers response (a json string containing {"result": "success"/"failure" }),
     	indicating whether the server successfully executed the query.
     *		NULL on failure.
     */
    
    char *mobile_set_pin(char *current_pin, char *new_pin);
    
    
    /**
     * Function: mobile_disable_pin
     *
     * Disables pin for the SIM card.
     *
     * Parameter:
     * 		pin - Currently active pin.
     *
     * Returns:
     *		A string containing the servers response (a json string containing {"result": "success"/"failure" }),
     	indicating whether the server successfully executed the query.
     *		NULL on failure.
     */
    
    char *mobile_disable_pin(char *pin);
    
    
    /**
     * Function: mobile_post_request
     *
     * Function that prepares curl environment and final POST query, then executes said query.
     *
     * Parameter:
     * 		query - The POST query content to execute.
     *
     * Returns:
     *		A string containing the servers response (a json string containing {"result": "success"/"failure" }),
     	indicating whether the server successfully executed the query.
     *		NULL on failure.
     */
    
    char *mobile_post_request(char *query);
    
    
    /**
     * Function: mobile_get_request
     *
     * Function that prepares curl environment and final GET query, then executes said query.
     *
     * Parameter:
     * 		vars - A string of comma separated values which to get from the server.
     *
     * Returns:
     *		A string containing the servers response (a json string containing {"result": "success"/"failure" }),
     	indicating whether the server successfully executed the query.
     *		NULL on failure.
     */
    
    char *mobile_get_request(char *vars);