PyObject *Mifare_write_block(Mifare * self, PyObject * args) { phStatus_t status = 0; uint8_t blockIdx; uint8_t *data; int dataLen; if (!PyArg_ParseTuple(args, "bs#", &blockIdx, &data, &dataLen)) { return NULL; } if (dataLen != PHAL_MFUL_WRITE_BLOCK_LENGTH) { return PyErr_Format(WriteError, "Write data MUST be specified as %d bytes", PHAL_MFUL_WRITE_BLOCK_LENGTH); } status = phalMful_Write(&salMfc, blockIdx, data); if (status != PH_ERR_SUCCESS) { return PyErr_Format(WriteError, "Write failed: %04x", status); } Py_RETURN_NONE; }
PyObject *Mifare_write_block(Mifare *self, PyObject *args) { uint16_t status; uint8_t blockIdx; uint8_t *data; int dataLen; #if PY_MAJOR_VERSION >= 3 if (!PyArg_ParseTuple(args, "by#", &blockIdx, &data, &dataLen)) { #else if (!PyArg_ParseTuple(args, "bs#", &blockIdx, &data, &dataLen)) { #endif return NULL; } if ((status = phalMful_Write(&self->data.alMful, blockIdx, data)) != 0) { return PyErr_Format(WriteError, "Write failed: %04x", status); } Py_RETURN_NONE; } /*********************************** ** Python Type Definiton ***********************************/ PyMethodDef Mifare_methods[] = { {"select", (PyCFunction) Mifare_select, METH_NOARGS, "Select a Mifare card if present. Returns the card UID"}, {"read_block", (PyCFunction) Mifare_read_block, METH_VARARGS, "Read 16 bytes starting at the specified block."}, {"write_block", (PyCFunction) Mifare_write_block, METH_VARARGS, "Write 4 bytes starting at the specified block."}, {NULL} /* Sentinel */ }; PyTypeObject MifareType = { PyVarObject_HEAD_INIT(NULL, 0) "nxppy.Mifare", /* tp_name */ sizeof(Mifare), /* tp_basicsize */ 0, /* tp_itemsize */ 0, /* tp_dealloc */ 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_reserved */ 0, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ 0, /* tp_as_mapping */ 0, /* tp_hash */ 0, /* tp_call */ 0, /* tp_str */ 0, /* tp_getattro */ 0, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT, /* tp_flags */ "Mifare objects", /* tp_doc */ 0, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ Mifare_methods, /* tp_methods */ 0, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ (initproc)Mifare_init, /* tp_init */ };