HAPError HAPIPAccessoryProtocolGetCharacteristicReadRequests()

in HAP/HAPIPAccessoryProtocol.c [152:281]


HAPError HAPIPAccessoryProtocolGetCharacteristicReadRequests(
        char* bytes,
        size_t numBytes,
        HAPIPReadContextRef* readContexts,
        size_t maxReadContexts,
        size_t* numReadContexts,
        HAPIPReadRequestParameters* parameters) {
    HAPError err;
    bool done;
    unsigned int x;
    uint64_t aid, iid;
    size_t i, k, n;
    HAPAssert(bytes != NULL);
    HAPAssert(readContexts != NULL);
    HAPAssert(numReadContexts != NULL);
    HAPAssert(parameters != NULL);
    *numReadContexts = 0;
    parameters->meta = false;
    parameters->perms = false;
    parameters->type = false;
    parameters->ev = false;
    err = kHAPError_None;
    i = 0;
    HAPAssert(i <= numBytes);
    while (!err && (i < numBytes)) {
        if ((numBytes - i >= 3) && (bytes[i] == 'i') && (bytes[i + 1] == 'd') && (bytes[i + 2] == '=')) {
            i += 3;
            HAPAssert(i <= numBytes);
            if (i < numBytes) {
                done = false;
                do {
                    HAPAssert(i <= numBytes);
                    k = i;
                    i += try_read_uint64(&bytes[i], numBytes - i, &aid);
                    HAPAssert(k <= i);
                    HAPAssert(i <= numBytes);
                    if ((k < i) && (i < numBytes) && (bytes[i] == '.')) {
                        i++;
                        k = i;
                        i += try_read_uint64(&bytes[i], numBytes - i, &iid);
                        HAPAssert(k <= i);
                        HAPAssert(i <= numBytes);
                        if ((k < i) && ((i == numBytes) || ((bytes[i] < '0') || (bytes[i] > '9')))) {
                            if (*numReadContexts < maxReadContexts) {
                                HAPIPReadContext* readContext = (HAPIPReadContext*) &readContexts[*numReadContexts];
                                HAPRawBufferZero(readContext, sizeof *readContext);
                                readContext->aid = aid;
                                readContext->iid = iid;
                                (*numReadContexts)++;
                            } else {
                                HAPAssert(*numReadContexts == maxReadContexts);
                                err = kHAPError_OutOfResources;
                            }
                            HAPAssert(i <= numBytes);
                            if ((i == numBytes) || (bytes[i] != ',')) {
                                done = true;
                            } else {
                                i++;
                            }
                        } else {
                            err = kHAPError_InvalidData;
                        }
                    } else {
                        err = kHAPError_InvalidData;
                    }
                } while (!err && !done);
            }
        } else if (
                (numBytes - i >= 5) && (bytes[i] == 'm') && (bytes[i + 1] == 'e') && (bytes[i + 2] == 't') &&
                (bytes[i + 3] == 'a') && (bytes[i + 4] == '=')) {
            i += 5;
            n = try_read_uint(&bytes[i], numBytes - i, &x);
            if ((n == 1) && ((x == 0) || (x == 1))) {
                parameters->meta = x != 0;
                i++;
            } else {
                err = kHAPError_InvalidData;
            }
        } else if (
                (numBytes - i >= 6) && (bytes[i] == 'p') && (bytes[i + 1] == 'e') && (bytes[i + 2] == 'r') &&
                (bytes[i + 3] == 'm') && (bytes[i + 4] == 's') && (bytes[i + 5] == '=')) {
            i += 6;
            n = try_read_uint(&bytes[i], numBytes - i, &x);
            if ((n == 1) && ((x == 0) || (x == 1))) {
                parameters->perms = x != 0;
                i++;
            } else {
                err = kHAPError_InvalidData;
            }
        } else if (
                (numBytes - i >= 5) && (bytes[i] == 't') && (bytes[i + 1] == 'y') && (bytes[i + 2] == 'p') &&
                (bytes[i + 3] == 'e') && (bytes[i + 4] == '=')) {
            i += 5;
            n = try_read_uint(&bytes[i], numBytes - i, &x);
            if ((n == 1) && ((x == 0) || (x == 1))) {
                parameters->type = x != 0;
                i++;
            } else {
                err = kHAPError_InvalidData;
            }
        } else if ((numBytes - i >= 3) && (bytes[i] == 'e') && (bytes[i + 1] == 'v') && (bytes[i + 2] == '=')) {
            i += 3;
            n = try_read_uint(&bytes[i], numBytes - i, &x);
            if ((n == 1) && ((x == 0) || (x == 1))) {
                parameters->ev = x != 0;
                i++;
            } else {
                err = kHAPError_InvalidData;
            }
        } else {
            err = kHAPError_InvalidData;
        }
        HAPAssert(i <= numBytes);
        if (!err && (i < numBytes)) {
            switch (bytes[i]) {
                case '&':
                    i++;
                    break;
                case '#':
                    numBytes = i;
                    break;
                default:
                    err = kHAPError_InvalidData;
                    break;
            }
        }
    }
    HAPAssert(err || (i == numBytes));
    return err;
}