HiLinkAPI module

exception HiLinkAPI.hilinkException(modemname, message)[source]

Bases: Exception

HiLink API exception

Parameters
  • modemname (string) – Unique name of the modem will be used in raising an exceptions to identify the respective modem

  • message (string) – Error message body for the raising exception

class HiLinkAPI.webui(modemname, host, username=None, password=None, logger=None)[source]

Bases: Thread

This class facilitate to open, authenticate and operate functions of supported Huawei HiLink modems.

Parameters
  • modemname – A uniquely identifiable name for the modem will useful when debugging or tracing with logs

  • host (string) – IP address of the modem

  • username (string, defaults to None) – Username if authentication required

  • password (string, defaults to None) – Password if authentication required

  • logger (logging.Logger, defaults to None) – Logger object if using already configured logging.Logger

buildCookies()[source]

This method will build a dictionary object containing SessionID which is provided as cookies to HTTP requests. Each call of httpGet() and httpPost() will generate default cookies set if cookies not provided in parameters.

Returns

Return a dictionary containing cookies

Return type

dictionary

configureDataConnection(roaming=True, maxIdleTime=0)[source]

This method will configure data connection properties

  • Switch on or off data roaming based on roaming.

  • Set max idle time out for the data connection

  • Rest of configurations keep as defaults

Parameters
  • roaming (bool) – Either set data roaming enabled or disabled

  • maxIdleTime (int, Default to 0 = Disabled) – Maximum idle timeout for the data connection

Returns

Return either data connection configuration succeeded or failed

Return type

bool

errorCodes = {100002: 'ERROR_SYSTEM_NO_SUPPORT', 100003: 'ERROR_SYSTEM_NO_RIGHTS', 100004: 'ERROR_BUSY', 101001: 'ERROR_NO_SIM_CARD_OR_INVALID_SIM_CARD', 101002: 'ERROR_CHECK_SIM_CARD_PIN_LOCK', 101003: 'ERROR_CHECK_SIM_CARD_PUN_LOCK', 101004: 'ERROR_CHECK_SIM_CARD_CAN_UNUSEABLE', 101005: 'ERROR_ENABLE_PIN_FAILED', 101006: 'ERROR_DISABLE_PIN_FAILED', 101007: 'ERROR_UNLOCK_PIN_FAILED', 101008: 'ERROR_DISABLE_AUTO_PIN_FAILED', 101009: 'ERROR_ENABLE_AUTO_PIN_FAILED', 103002: 'ERROR_DEVICE_PIN_VALIDATE_FAILED', 103003: 'ERROR_DEVICE_PIN_MODIFFY_FAILED', 103004: 'ERROR_DEVICE_PUK_MODIFFY_FAILED', 103008: 'ERROR_DEVICE_SIM_CARD_BUSY', 103009: 'ERROR_DEVICE_SIM_LOCK_INPUT_ERROR', 103011: 'ERROR_DEVICE_PUK_DEAD_LOCK', 108001: 'ERROR_LOGIN_USERNAME_WRONG', 108002: 'ERROR_LOGIN_PASSWORD_WRONG', 108003: 'ERROR_LOGIN_ALREADY_LOGIN', 108005: 'ERROR_LOGIN_TOO_MANY_USERS_LOGINED', 108006: 'ERROR_LOGIN_USERNAME_OR_PASSWORD_ERROR', 108007: 'ERROR_LOGIN_TOO_MANY_TIMES', 108008: 'MODIFYPASSWORD_ERROR', 108009: 'ERROR_LOGIN_IN_DEFFERENT_DEVICES', 108010: 'ERROR_LOGIN_FREQUENTLY_LOGIN', 112001: 'ERROR_SET_NET_MODE_AND_BAND_WHEN_DAILUP_FAILED', 112002: 'ERROR_SET_NET_SEARCH_MODE_WHEN_DAILUP_FAILED', 112003: 'ERROR_SET_NET_MODE_AND_BAND_FAILED', 112005: 'ERROR_NET_REGISTER_NET_FAILED', 112008: 'ERROR_NET_SIM_CARD_NOT_READY_STATUS', 125001: 'ERROR_WRONG_TOKEN', 125002: 'ERROR_WRONG_SESSION', 125003: 'ERROR_WRONG_SESSION_TOKEN'}
getActiveError()[source]

This method will return if theres any active error code else none

Returns

Return {“errorcode”:<code>,”error”:”<error message>”}

Return type

dictionary

getDataRoaming()[source]

This method will return either data roaming enabled or disabled.

Mandatory to update by data configuration info by calling queryDataConnection() prior to call this method

Returns

Return data roaming enabled or not

Return type

bool

getDeviceInfo()[source]

This method will return following device info as a dictionary.

These information have get update by calling queryDeviceInfo() (Required only one time as these are constants)

  1. devicename - Device name

  2. serial - Modem serial number

  3. imei - IMEI number of the modem

  4. imsi - IMSI number

  5. iccid - ICCID of the SIM

  6. modes - Supported network modes (LTE,WCDMA,GSM)

  7. hwversion - Hardware version of the modem

  8. swversion - Software version of the modem

  9. webui - WebUI version of the modem

Returns

Device information as a dictionary

Return type

dictionary

getDeviceName()[source]

This method will return the device name (model) from API end point /api/device/information.

Returns

Return device name

Return type

string

getKnownErrors()[source]

This method will return all known errors

Returns

Return {“<error code>”:”<error message>”…}

Return type

dictionary

getLoginRequired()[source]

This method will return either login/authentication required or not which will be updated after calling initialize().

Returns

Return login required or not

Return type

bool

getLoginWaitTime()[source]

This method will return waittime for next login attemp

If session need a refresh validateSession() before calling device information API end point.

Returns

Login wait time

Return type

int

getMaxIdleTime()[source]

This method will return max idle time out of the data connection.

Mandatory to update by data configuration info by calling queryDataConnection() prior to call this method

Returns

Return max data connection idle time

Return type

int

getNetwokModes()[source]

Get primary and secondary network modes

Returns

{“primary”:<<Primary networn mode>>,”secondary”:<<Secondary networn mode>>}

Return type

dictionary

getNetwork()[source]

This method will return the name of Carrier Network.

Carrier Network name can update by calling queryNetwork() and required only to call one time after the first time after a possible WAN IP change like,

  1. Connection switch on/off event after calling switchConnection().

  2. Switching between LTE and WCDMA even after calling switchLTE().

Returns

Return network name

Return type

string

getSessionRefreshInteval()[source]

This method will return the session refresh interval while in idle without any operation. Use setSessionRefreshInteval()

Returns

Session refresh interval in seconds

Return type

int

getValidSession()[source]

This method will return if the session is valid for querying and operations or not

Returns

Return a valid session or not

Return type

boolean

getWANIP()[source]

This method will return the WAN IP.

WAN IP can update by calling queryWANIP() and call this when after a possible WAN IP change like,

  1. Connection switch on/off event after calling switchConnection().

  2. Switching between LTE and WCDMA even after calling switchLTE().

Returns

Return WAN IP

Return type

string

getWebUIVersion()[source]

This method will return WebUI version either 10, 17 or 21.

Returns

Return WebUI version

Return type

int

getWorkmode()[source]

This method will return the work mode.

Mandatory to update by device work mode by calling queryDeviceInfo() prior to call this method

Returns

Return network name

Return type

string

httpGet(endpoint, cookies=None, headers=None)[source]

Call an API end point using a HTTP GET request. If cookies are not provided (when defaulted to None) will build cookies by calling buildCookies(). At the end of each call processHTTPHeaders() will call to retrieve SessionID and __RequestVerificationToken.

Parameters
  • endpoint (string) – API end point (eg:- /api/device/information)

  • cookies (dictionary) – cookies, defaults to None

  • headers (dictionary) – HTTP headers, defaults to None

Returns

Return the HTTP response as a requests.Response

Return type

requests.Response

httpPost(endpoint, postBody, cookies=None, headers=None)[source]

Call an API end point using a HTTP POST request. If cookies are not provided (when defaulted to None) will build cookies by calling buildCookies(). At the end of each call processHTTPHeaders() will call to retrieve SessionID and __RequestVerificationToken.

Parameters
  • endpoint (string) – API end point (eg:- /api/user/authentication_login)

  • postBody (string) – HTTP body

  • cookies (dictionary) – cookies

  • headers (dictionary) – HTTP headers

Returns

Return the HTTP response as a requests.Response

Return type

requests.Response

initialize()[source]

Calling this method will initialize API calls by

  • Initialize session and fetch SessionID

  • Request initial __RequestVerificationToken

  • Query authentication required or not

  • Identify webUI version

isStopped()[source]

This method will return successfully stopped or not.

Returns

Return deinited or not

Return type

boolean

login_b64_sha256(data)[source]

This method will used to SHA256 hashing and base64 encoding for WebUI version 10.x.x authentication in login_WebUI10().

Parameters

data (string) – Data to hash and encode

Returns

Return hashed and encoded data string

Return type

string

processHTTPHeaders(response)[source]

This method will retrieve SessionID from cookies and __RequestVerificationToken from HTTP headers. This method has to be called after each requests.get or requests.post as mismatch with SessionID or __RequestVerificationToken between API(webui) and HTTP request call leading to return errors in API calls.

Parameters

response (requests.Response) – Response object from requests.get or requests.post

queryDataConnection()[source]

This method will query following data connection properties and update existing.

  • Data roaming enabled or disabled

  • Max connection idle timeout

If session need a refresh validateSession() before calling device information API end point.

Returns

Return querying data connection properties succeed or not

Return type

boolean

queryDeviceInfo()[source]

This method will query device information and update existing.

If session need a refresh validateSession() before calling device information API end point.

Returns

Return querying device info succeed or not

Return type

boolean

queryNetwork()[source]

This method will query network name of the carrier network and update existing.

If session need a refresh validateSession() before calling device information API end point.

Returns

Return querying network succeed or not

Return type

boolean

querySupportedNetworkMethods()[source]

This method will query supported network modes

If session need a refresh validateSession() before calling device information API end point.

Returns

Return querying supported network modes succeeded or not

Return type

boolean

queryWANIP()[source]

This method will query WAN IP from the carrier network and update existing.

If session need a refresh validateSession() before calling device information API end point.

Separate API end points will be called as per the WebUI version.

Returns

Return querying WAN IP succeed or not

Return type

boolean

reboot()[source]

Reboot the modem. Return only reboot initiation success or not only as reboot doesnot returns any.

Returns

Return reboot initiation success or not

Return type

bool

resetActiveErrorCode()[source]

This method will reset active error code

run()[source]

This is the overriding method for :class:threading.Thread.run()

  • Check login state of the session

  • Perform login when required

sessionErrorCheck(responseDict)[source]

This method will use to validate error responses

setCredentials(username, password)[source]

This method will set/update username and password for authentication after initializing.

Parameters
  • username (string, defaults to None) – Username if authentication required

  • password (string, defaults to None) – Password if authentication required

setNetwokModes(primary='LTE', secondary='WCDMA')[source]

Set primary and secondary network modes respectively with primary and secondary.

Parameters
  • primary (String) – Either “LTE”,”WCDMA” or “GSM” as primary network mode

  • secondary (String) – Either “LTE”,”WCDMA” or “GSM” as secondary network mode

Returns

Return network mode configuration success or not

Return type

bool

setSessionRefreshInteval(interval)[source]

This method will set the session refresh interval while in idle without any operation.

Parameters

interval (int) – Session refresh interval in seconds

start()[source]

This method will start the thread.

stop()[source]

This method will initialize thread stop.

switchConnection(status=True)[source]

Switch on or off data connection based on status.

Parameters

status (bool) – Either set status of the connection On or Off

Returns

Return either requested connection switching succeeded or failed

Return type

bool

switchDHCPIPBlock(gateway)[source]

This methos will change DHCP IP block and gateway(modem) IP based on provided gateway.

All existing connections will drop and probably a soft reboot will performed after calling this method.

Use only gateway in 192.168.X.1 format (eg:- 192.168.2.1, 192.168.20.1). So the DHCP offering IP block will be 192.168.x.100-192.168.x.199.

Parameters

gateway (string) – Gateway or IP of the modem

switchNetworMode(primary=True)[source]

Switch network between primary and secondary network modes based on primary. If primary is True network mode switch to the primary or else to the secondary.

Primary network mode and secondary network mode can be set using setNetwokModes()

Parameters

primary (bool) – Primary network mode or secondary network mode

Returns

Return either requested network mode switching succeeded or failed

Return type

bool

validateSession()[source]

This method will validate session

  • Check if a valid authenticated session

  • If authentication required will login

Returns

Return valid session or not

Return type

boolean